UNPKG

did-jwt

Version:

Library for Signing and Verifying JWTs that use DIDs as issuers and JWEs that use DIDs as recipients

43 lines • 2.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.keccak = exports.ripemd160 = void 0; exports.sha256 = sha256; exports.toEthereumAddress = toEthereumAddress; exports.concatKDF = concatKDF; const sha2_js_1 = require("@noble/hashes/sha2.js"); const sha3_js_1 = require("@noble/hashes/sha3.js"); const uint8arrays_1 = require("uint8arrays"); var legacy_js_1 = require("@noble/hashes/legacy.js"); Object.defineProperty(exports, "ripemd160", { enumerable: true, get: function () { return legacy_js_1.ripemd160; } }); function sha256(payload) { const data = typeof payload === 'string' ? (0, uint8arrays_1.fromString)(payload) : payload; return (0, sha2_js_1.sha256)(data); } exports.keccak = sha3_js_1.keccak_256; function toEthereumAddress(hexPublicKey) { const hashInput = (0, uint8arrays_1.fromString)(hexPublicKey.slice(2), 'base16'); return `0x${(0, uint8arrays_1.toString)((0, exports.keccak)(hashInput).slice(-20), 'base16')}`; } function writeUint32BE(value, array = new Uint8Array(4)) { const encoded = (0, uint8arrays_1.fromString)(value.toString(), 'base10'); array.set(encoded, 4 - encoded.length); return array; } const lengthAndInput = (input) => (0, uint8arrays_1.concat)([writeUint32BE(input.length), input]); // This implementation of concatKDF was inspired by these two implementations: // https://github.com/digitalbazaar/minimal-cipher/blob/master/algorithms/ecdhkdf.js // https://github.com/panva/jose/blob/master/lib/jwa/ecdh/derive.js function concatKDF(secret, keyLen, alg, producerInfo, consumerInfo) { if (keyLen !== 256) throw new Error(`Unsupported key length: ${keyLen}`); const value = (0, uint8arrays_1.concat)([ lengthAndInput((0, uint8arrays_1.fromString)(alg)), lengthAndInput(typeof producerInfo === 'undefined' ? new Uint8Array(0) : producerInfo), // apu lengthAndInput(typeof consumerInfo === 'undefined' ? new Uint8Array(0) : consumerInfo), // apv writeUint32BE(keyLen), ]); // since our key lenght is 256 we only have to do one round const roundNumber = 1; return sha256((0, uint8arrays_1.concat)([writeUint32BE(roundNumber), secret, value])); } //# sourceMappingURL=Digest.js.map