0
1

More than 1 year has passed since last update.

タイムスタンプ検証

Last updated at Posted at 2023-06-19
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

public class TimestampAuthentication {
    private static final String ALGORITHM = "SHA256withRSA";

    public static boolean verifyTimestamp(String message, String timestamp, String signature, String publicKey) {
        try {
            byte[] publicKeyBytes = Base64.getDecoder().decode(publicKey);
            byte[] signatureBytes = Base64.getDecoder().decode(signature);

            PublicKey pubKey = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(publicKeyBytes));

            Signature verifier = Signature.getInstance(ALGORITHM);
            verifier.initVerify(pubKey);
            verifier.update(message.getBytes());

            return verifier.verify(signatureBytes);
        } catch (NoSuchAlgorithmException | InvalidKeySpecException | InvalidKeyException | SignatureException e) {
            e.printStackTrace();
        }

        return false;
    }

    public static void main(String[] args) {
        String message = "Hello, world!";
        String timestamp = "2023-06-18T12:34:56";
        String signature = "SIGNATURE"; // クライアントが署名したデータ
        String publicKey = "PUBLIC_KEY"; // サーバーで共有された公開鍵

        boolean isVerified = verifyTimestamp(message, timestamp, signature, publicKey);
        System.out.println("Timestamp Verification: " + isVerified);
    }
}
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.pdfbox.pdmodel.PDDocument;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class PDFValidation {
    public static void main(String[] args) {
        try {
            // PDFを読み込む
            PDDocument document = PDDocument.load(new File("path/to/your/pdf/file.pdf"));

            // タイムスタンプデータからトークン生成
            String timestampData = "2023-07-10 12:34:56"; // タイムスタンプデータの例
            byte[] timestampBytes = timestampData.getBytes();
            byte[] tokenBytes = MessageDigest.getInstance("SHA-256").digest(timestampBytes);
            String token = DigestUtils.sha256Hex(timestampBytes); // Apache Commons Codecを使用する場合

            // 生成されたトークンを利用して適切な処理を実行

            // メモリリークを防ぐために必ずドキュメントを閉じる
            document.close();
        } catch (IOException | NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }
}
import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.common.PDStream;
import org.apache.pdfbox.pdmodel.graphics.PDXObject;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.apache.pdfbox.pdmodel.font.PDFont;

import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class PDFValidation {
    public static void main(String[] args) {
        try {
            // PDFを読み込む
            PDDocument document = PDDocument.load(new File("path/to/your/pdf/file.pdf"));

            // トークン再生成とハッシュ値検証
            for (int pageNum = 0; pageNum < document.getNumberOfPages(); pageNum++) {
                PDPage page = document.getPage(pageNum);
                
                // テキストトークンの再生成
                PDFTextStripper stripper = new PDFTextStripper();
                stripper.setStartPage(pageNum + 1);
                stripper.setEndPage(pageNum + 1);
                String text = stripper.getText(document);
                byte[] textBytes = text.getBytes();
                byte[] textHash = MessageDigest.getInstance("SHA-256").digest(textBytes);
                // ハッシュ値の検証など、適切な処理を実行
                
                // 画像トークンの再生成
                for (COSName key : page.getResources().getXObjectNames()) {
                    PDXObject object = page.getResources().getXObject(key);
                    if (object instanceof PDImageXObject) {
                        PDImageXObject imageObject = (PDImageXObject) object;
                        COSDictionary dictionary = imageObject.getCOSObject();
                        PDStream stream = new PDStream(dictionary);
                        byte[] imageBytes = stream.toByteArray();
                        byte[] imageHash = MessageDigest.getInstance("SHA-256").digest(imageBytes);
                        // ハッシュ値の検証など、適切な処理を実行
                    }
                }
                
                // フォントトークンの再生成
                for (COSName key : page.getResources().getFontNames()) {
                    PDFont font = page.getResources().getFont(key);
                    COSDictionary dictionary = font.getCOSObject();
                    PDStream stream = new PDStream(dictionary);
                    byte[] fontBytes = stream.toByteArray();
                    byte[] fontHash = MessageDigest.getInstance("SHA-256").digest(fontBytes);
                    // ハッシュ値の検証など、適切な処理を実行
                }
            }
            
            // メモリリークを防ぐために必ずドキュメントを閉じる
            document.close();
        } catch (IOException | NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }
}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1