LoginSignup
0
0

More than 1 year has passed since last update.

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

Posted at

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

jsrsasign のダウンロード

NPMでダウンロードします。

> npm install jsrsasign jsrsasign-util

サンプルプログラム

KJUR.asn1.tsp.TimeStampReq, KJUR.asn1.tsp.MessageImprint を使用して、タイムスタンプリクエストを作成します。

const {createHash, } = require('node:crypto');
const {KJUR, ASN1HEX} = require('jsrsasign');


//ハッシュ値の生成
const hash = createHash('sha256');
hash.update('Hello World');

//MessageImprintの作成
var messageImprint = new KJUR.asn1.tsp.MessageImprint({
    alg: 'sha256',
    hash: hash.digest('hex')
});

//TimeStampReqの作成
var timestampReq = new KJUR.asn1.tsp.TimeStampReq({
    messageImprint: messageImprint,
    policy: null,
    nonce: null,
    certreq: true
});

//ファイルへ出力
const buf_arr = Uint8Array.from(Buffer.from(timestampReq.tohex(), "hex"));
const fs = require('fs');
fs.writeFile('HelloWorld.req', buf_arr, err => {
    if (err) throw err;
});

//TimeStampReqを解析した内容を表示
console.log(ASN1HEX.dump(timestampReq));

実行結果

プログラムを実行した結果です。

>node HelloWorld_TimestampReq.js
SEQUENCE
  INTEGER 01
  SEQUENCE
    SEQUENCE
      ObjectIdentifier sha256 (2 16 840 1 101 3 4 2 1)
      NULL
    OCTETSTRING a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e
  BOOLEAN TRUE
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