UNPKG

js-crypto-key-utils

Version:

Universal Module for Cryptographic Key Utilities in JavaScript, including PEM-JWK converters

119 lines 5.56 kB
"use strict"; /** * octenc.js */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.toJwk = exports.fromJwk = void 0; var params = __importStar(require("./params")); var js_encoding_utils_1 = __importDefault(require("js-encoding-utils")); var elliptic = __importStar(require("elliptic")); var util_1 = require("./util"); var Ec = elliptic.ec; /** * Convert JWK EC public/private keys to octet form. * Compressed form of EC public key is referred to RFC 5480 {@link https://tools.ietf.org/html/rfc5480}. * @param {JsonWebKey} jwkey - A key object in JWK format to be encoded to SEC1 octet format key. * @param {boolean} [outputPublic] - Export public key even from private key if true. * @param {OctetFormat} [outputFormat='binary'] - 'binary' or 'string'. * @param {boolean} [compact=false] - Export compressed form of public key if true. * @return {Uint8Array|string} - Encoded key object in JWK format. */ var fromJwk = function (jwkey, _a) { var outputPublic = _a.outputPublic, _b = _a.outputFormat, outputFormat = _b === void 0 ? 'binary' : _b, _c = _a.compact, compact = _c === void 0 ? false : _c; // original key type var orgType = (0, util_1.getJwkType)(jwkey); var type = (typeof outputPublic === 'boolean' && outputPublic) ? 'public' : orgType; if (type === 'public') { var bufX = js_encoding_utils_1.default.encoder.decodeBase64Url(jwkey.x); var bufY = js_encoding_utils_1.default.encoder.decodeBase64Url(jwkey.y); var publicKey = void 0; if (compact) { // compressed form // http://www.secg.org/SEC1-Ver-1.0.pdf publicKey = new Uint8Array(bufX.length + 1); publicKey[0] = 0xFF & ((0x01 & (bufY.slice(-1)[0])) + 0x02); publicKey.set(bufX, 1); } else { // uncompressed form publicKey = new Uint8Array(bufX.length + bufY.length + 1); publicKey[0] = 0xFF & 0x04; publicKey.set(bufX, 1); publicKey.set(bufY, bufX.length + 1); } return (outputFormat === 'string') ? js_encoding_utils_1.default.encoder.arrayBufferToHexString(publicKey) : publicKey; } else { //type === 'private' if (!jwkey.d) throw new Error('InvalidKey'); var bufD = js_encoding_utils_1.default.encoder.decodeBase64Url(jwkey.d); return (outputFormat === 'string') ? js_encoding_utils_1.default.encoder.arrayBufferToHexString(bufD) : bufD; } }; exports.fromJwk = fromJwk; /** * Convert Octet form of EC public/private keys to JWK. * @param {OctetEC} octkey - OctetEC key object in hex string format or Uint8Array. * @param {String} namedCurve - Name of elliptic curve like 'P-256'. * @param {boolean} [outputPublic] - Export public key even from private key if true. * @return {JsonWebKey} - Derived key object in JWK format. */ var toJwk = function (octkey, namedCurve, _a) { var outputPublic = _a.outputPublic; if (Object.keys(params.namedCurves).indexOf(namedCurve) < 0) throw new Error('UnsupportedCurve'); // original key type and check the key structure var orgType = (0, util_1.getSec1KeyType)(octkey, namedCurve); var type = (typeof outputPublic === 'boolean' && outputPublic) ? 'public' : orgType; // format conversion var binKey = (typeof octkey === 'string') ? js_encoding_utils_1.default.encoder.hexStringToArrayBuffer(octkey) : octkey; // instantiation var curve = params.namedCurves[namedCurve].indutnyName; var ec = new Ec(curve); // derive key object from binary key var ecKey = (orgType === 'public') ? ec.keyFromPublic(binKey) : ec.keyFromPrivate(binKey); var publicKey = new Uint8Array(ecKey.getPublic('array')); var len = params.namedCurves[namedCurve].payloadSize; var bufX = publicKey.slice(1, len + 1); var bufY = publicKey.slice(len + 1, len * 2 + 1); var jwKey = { kty: 'EC', crv: namedCurve, x: js_encoding_utils_1.default.encoder.encodeBase64Url(bufX), y: js_encoding_utils_1.default.encoder.encodeBase64Url(bufY) // ext: true }; if (type === 'private') { // octkey is exactly private key if type is private. jwKey.d = js_encoding_utils_1.default.encoder.encodeBase64Url(binKey); } return jwKey; }; exports.toJwk = toJwk; //# sourceMappingURL=octenc.js.map