UNPKG

did-jwt

Version:

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

36 lines • 1.76 kB
import { sha256 as sha256Hash } from '@noble/hashes/sha2.js'; import { keccak_256 } from '@noble/hashes/sha3.js'; import { concat, fromString, toString } from 'uint8arrays'; export { ripemd160 } from '@noble/hashes/legacy.js'; export function sha256(payload) { const data = typeof payload === 'string' ? fromString(payload) : payload; return sha256Hash(data); } export const keccak = keccak_256; export function toEthereumAddress(hexPublicKey) { const hashInput = fromString(hexPublicKey.slice(2), 'base16'); return `0x${toString(keccak(hashInput).slice(-20), 'base16')}`; } function writeUint32BE(value, array = new Uint8Array(4)) { const encoded = fromString(value.toString(), 'base10'); array.set(encoded, 4 - encoded.length); return array; } const lengthAndInput = (input) => 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 export function concatKDF(secret, keyLen, alg, producerInfo, consumerInfo) { if (keyLen !== 256) throw new Error(`Unsupported key length: ${keyLen}`); const value = concat([ lengthAndInput(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(concat([writeUint32BE(roundNumber), secret, value])); } //# sourceMappingURL=Digest.js.map