@isomorphic-pgp/parser
Version:
An OpenPGP message parser
23 lines (20 loc) • 1.47 kB
JavaScript
const concatenate = require("concat-buffers");
// prettier-ignore
const EMSA_PKCS1_v1_5_HASH_PREFIX = {
MD5: new Uint8Array([0x30, 0x20, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10]),
'RIPEMD-160': new Uint8Array([0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x24, 0x03, 0x02, 0x01, 0x05, 0x00, 0x04, 0x14]),
SHA1: new Uint8Array([0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14]),
SHA224: new Uint8Array([0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04, 0x05, 0x00, 0x04, 0x1C]),
SHA256: new Uint8Array([0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20]),
SHA384: new Uint8Array([0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30]),
SHA512: new Uint8Array([0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40]),
};
module.exports.encode = function encode(hashType, hash, length) {
const T = EMSA_PKCS1_v1_5_HASH_PREFIX[hashType];
if (!T) throw new Error(`Unsupported hash "${hashType}"`)
const tLen = T.length + hash.length;
const paddingLength = length - tLen - 3;
const padding = new Uint8Array(paddingLength);
padding.fill(255);
return concatenate([new Uint8Array([0, 1]), padding, new Uint8Array([0]), T, hash]);
}