UNPKG

react-native-quick-crypto

Version:

A fast implementation of Node's `crypto` module written in C/C++ JSI

101 lines (99 loc) 3.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Dsa = void 0; exports.dsa_generateKeyPairNode = dsa_generateKeyPairNode; exports.dsa_generateKeyPairNodeSync = dsa_generateKeyPairNodeSync; var _reactNativeNitroModules = require("react-native-nitro-modules"); var _reactNativeBuffer = require("@craftzdog/react-native-buffer"); var _classes = require("./keys/classes"); var _utils = require("./utils"); class Dsa { constructor(modulusLength, divisorLength) { this.native = _reactNativeNitroModules.NitroModules.createHybridObject('DsaKeyPair'); this.native.setModulusLength(modulusLength); if (divisorLength !== undefined && divisorLength >= 0) { this.native.setDivisorLength(divisorLength); } } async generateKeyPair() { await this.native.generateKeyPair(); } generateKeyPairSync() { this.native.generateKeyPairSync(); } } // FIPS 186-4 §4.2: only L = 1024, 2048, 3072 are sanctioned. NIST has // deprecated DSA-1024 for new applications, but we retain it for // interop with legacy systems and match Node's permissive default. We // reject anything below 1024 outright. exports.Dsa = Dsa; const DSA_MIN_MODULUS_LENGTH = 1024; function dsa_prepareKeyGenParams(options) { if (!options) { throw new Error('Options are required for DSA key generation'); } const { modulusLength, divisorLength } = options; if (!modulusLength || modulusLength < DSA_MIN_MODULUS_LENGTH) { throw new RangeError(`DSA modulusLength must be at least ${DSA_MIN_MODULUS_LENGTH} bits ` + `(got ${modulusLength ?? 0})`); } return new Dsa(modulusLength, divisorLength); } function dsa_formatKeyPairOutput(dsa, encoding) { const { publicFormat, privateFormat, cipher, passphrase } = encoding; const publicKeyData = dsa.native.getPublicKey(); const privateKeyData = dsa.native.getPrivateKey(); const pub = _classes.KeyObject.createKeyObject('public', publicKeyData, _utils.KFormatType.DER, _utils.KeyEncoding.SPKI); const priv = _classes.KeyObject.createKeyObject('private', privateKeyData, _utils.KFormatType.DER, _utils.KeyEncoding.PKCS8); let publicKey; let privateKey; if (publicFormat === 'raw-public' || privateFormat === 'raw-private' || privateFormat === 'raw-seed') { throw new Error('Raw key formats are not supported for DSA keys'); } if (publicFormat === -1) { publicKey = pub; } else { const format = publicFormat === _utils.KFormatType.PEM ? _utils.KFormatType.PEM : _utils.KFormatType.DER; const exported = pub.handle.exportKey(format, _utils.KeyEncoding.SPKI); if (format === _utils.KFormatType.PEM) { publicKey = _reactNativeBuffer.Buffer.from(new Uint8Array(exported)).toString('utf-8'); } else { publicKey = exported; } } if (privateFormat === -1) { privateKey = priv; } else { const format = privateFormat === _utils.KFormatType.PEM ? _utils.KFormatType.PEM : _utils.KFormatType.DER; const exported = priv.handle.exportKey(format, _utils.KeyEncoding.PKCS8, cipher, passphrase); if (format === _utils.KFormatType.PEM) { privateKey = _reactNativeBuffer.Buffer.from(new Uint8Array(exported)).toString('utf-8'); } else { privateKey = exported; } } return { publicKey, privateKey }; } async function dsa_generateKeyPairNode(options, encoding) { const dsa = dsa_prepareKeyGenParams(options); await dsa.generateKeyPair(); return dsa_formatKeyPairOutput(dsa, encoding); } function dsa_generateKeyPairNodeSync(options, encoding) { const dsa = dsa_prepareKeyGenParams(options); dsa.generateKeyPairSync(); return dsa_formatKeyPairOutput(dsa, encoding); } //# sourceMappingURL=dsa.js.map