UNPKG

react-native-quick-crypto

Version:

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

112 lines (111 loc) 4.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DhKeyPairGen = void 0; exports.dh_generateKeyPairNode = dh_generateKeyPairNode; exports.dh_generateKeyPairNodeSync = dh_generateKeyPairNodeSync; var _reactNativeNitroModules = require("react-native-nitro-modules"); var _reactNativeBuffer = require("@craftzdog/react-native-buffer"); var _classes = require("./keys/classes"); var _utils = require("./utils"); var _dhGroups = require("./dh-groups"); class DhKeyPairGen { constructor(options) { this.native = _reactNativeNitroModules.NitroModules.createHybridObject('DhKeyPair'); const { groupName, prime, primeLength, generator } = options; if (groupName) { // Resolve named group to prime + generator const group = _dhGroups.DH_GROUPS[groupName]; if (!group) { throw new Error(`Unknown DH group: ${groupName}`); } const primeBuf = _reactNativeBuffer.Buffer.from(group.prime, 'hex'); this.native.setPrime(primeBuf.buffer.slice(primeBuf.byteOffset, primeBuf.byteOffset + primeBuf.byteLength)); const gen = parseInt(group.generator, 16); this.native.setGenerator(gen); } else if (prime) { // Custom prime as Buffer const primeBuf = _reactNativeBuffer.Buffer.from(prime); this.native.setPrime(primeBuf.buffer.slice(primeBuf.byteOffset, primeBuf.byteOffset + primeBuf.byteLength)); this.native.setGenerator(generator ?? 2); } else if (primeLength) { this.native.setPrimeLength(primeLength); this.native.setGenerator(generator ?? 2); } else { throw new Error('DH key generation requires one of: groupName, prime, or primeLength'); } } async generateKeyPair() { await this.native.generateKeyPair(); } generateKeyPairSync() { this.native.generateKeyPairSync(); } } exports.DhKeyPairGen = DhKeyPairGen; function dh_prepareKeyGenParams(options) { if (!options) { throw new Error('Options are required for DH key generation'); } return new DhKeyPairGen(options); } function dh_formatKeyPairOutput(dh, encoding) { const { publicFormat, privateFormat, cipher, passphrase } = encoding; const publicKeyData = dh.native.getPublicKey(); const privateKeyData = dh.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 DH 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 dh_generateKeyPairNode(options, encoding) { const dh = dh_prepareKeyGenParams(options); await dh.generateKeyPair(); return dh_formatKeyPairOutput(dh, encoding); } function dh_generateKeyPairNodeSync(options, encoding) { const dh = dh_prepareKeyGenParams(options); dh.generateKeyPairSync(); return dh_formatKeyPairOutput(dh, encoding); } //# sourceMappingURL=dhKeyPair.js.map