did-jwt
Version:
Library for Signing and Verifying JWTs that use DIDs as issuers and JWEs that use DIDs as recipients
30 lines • 1.28 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ES256Signer = ES256Signer;
const util_js_1 = require("../util.js");
const Digest_js_1 = require("../Digest.js");
const nist_js_1 = require("@noble/curves/nist.js");
/**
* Creates a configured signer function for signing data using the ES256 (secp256r1 + sha256) algorithm.
*
* The signing function itself takes the data as a `Uint8Array` or `string` and returns a `base64Url`-encoded signature
*
* @example
* ```typescript
* const sign: Signer = ES256Signer(process.env.PRIVATE_KEY)
* const signature: string = await sign(data)
* ```
*
* @param {String} privateKey a private key as `Uint8Array`
* @return {Function} a configured signer function `(data: string | Uint8Array): Promise<string>`
*/
function ES256Signer(privateKey) {
if (privateKey.length !== 32) {
throw new Error(`bad_key: Invalid private key format. Expecting 32 bytes, but got ${privateKey.length}`);
}
return async (data) => {
const signature = nist_js_1.p256.sign((0, Digest_js_1.sha256)(data), privateKey, { prehash: false, lowS: false });
return (0, util_js_1.bytesToBase64url)(signature);
};
}
//# sourceMappingURL=ES256Signer.js.map