LoginSignup
0
0

More than 1 year has passed since last update.

Javaでタイムスタンプリクエストを作成する

Posted at

Javaの標準ライブラリにはタイムスタンプリクエストを生成するクラスは存在しないので、今回はBouncy Castle Crypto APIsを使用してタイムスタンプリクエストを生成します。

Bouncy Castle Crypto APIs のダウンロード

LATEST JAVA RELEASESから最新版をダウンロードします。

タイムスタンプリクエストを作成するために必要なjarファイルは、Provider, ASN.1 Utility Classes, PKIX/CMS/EAC/PKCS
OCSP/TSP/OPENSSL の jar ファイルになります。

Javaでタイムスタンプリクエストを作成するには、org.bouncycastle.tsp.TimeStampRequestGeneratorを利用します。

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

import org.bouncycastle.tsp.TSPAlgorithms;
import org.bouncycastle.tsp.TimeStampRequest;
import org.bouncycastle.tsp.TimeStampRequestGenerator;

public class HelloWorld_TimeStampReq {

	public static void main(String[] args) {

		//ハッシュ値の生成 SHA-256
		try {
			MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
			byte[] sha256Byte = sha256.digest("Hello World".getBytes());
		
			TimeStampRequestGenerator reqGen = new TimeStampRequestGenerator();
			reqGen.setCertReq(true);
			TimeStampRequest req = reqGen.generate(TSPAlgorithms.SHA256, sha256Byte);
			
			FileOutputStream fos = new FileOutputStream("HelloWorld.req");
			fos.write(req.getEncoded());
			fos.close();
			
		}
		catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
		catch (Throwable e) {
			e.printStackTrace();
		}
	}

}
0
0
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
0