react-native-quick-crypto
Version:
A fast implementation of Node's `crypto` module written in C/C++ JSI
70 lines (69 loc) • 2.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.SlhDsa = exports.SLH_DSA_VARIANTS = void 0;
exports.slhdsa_generateKeyPairWebCrypto = slhdsa_generateKeyPairWebCrypto;
var _reactNativeNitroModules = require("react-native-nitro-modules");
var _classes = require("./keys/classes");
var _utils = require("./utils");
const SLH_DSA_VARIANTS = exports.SLH_DSA_VARIANTS = ['SLH-DSA-SHA2-128s', 'SLH-DSA-SHA2-128f', 'SLH-DSA-SHA2-192s', 'SLH-DSA-SHA2-192f', 'SLH-DSA-SHA2-256s', 'SLH-DSA-SHA2-256f', 'SLH-DSA-SHAKE-128s', 'SLH-DSA-SHAKE-128f', 'SLH-DSA-SHAKE-192s', 'SLH-DSA-SHAKE-192f', 'SLH-DSA-SHAKE-256s', 'SLH-DSA-SHAKE-256f'];
class SlhDsa {
constructor(variant) {
this.variant = variant;
this.native = _reactNativeNitroModules.NitroModules.createHybridObject('SlhDsaKeyPair');
this.native.setVariant(variant);
}
async generateKeyPair() {
await this.native.generateKeyPair(_utils.KFormatType.DER, _utils.KeyEncoding.SPKI, _utils.KFormatType.DER, _utils.KeyEncoding.PKCS8);
}
generateKeyPairSync() {
this.native.generateKeyPairSync(_utils.KFormatType.DER, _utils.KeyEncoding.SPKI, _utils.KFormatType.DER, _utils.KeyEncoding.PKCS8);
}
getPublicKey() {
return this.native.getPublicKey();
}
getPrivateKey() {
return this.native.getPrivateKey();
}
async sign(message) {
return this.native.sign(message);
}
signSync(message) {
return this.native.signSync(message);
}
async verify(signature, message) {
return this.native.verify(signature, message);
}
verifySync(signature, message) {
return this.native.verifySync(signature, message);
}
}
exports.SlhDsa = SlhDsa;
async function slhdsa_generateKeyPairWebCrypto(variant, extractable, keyUsages) {
if ((0, _utils.hasAnyNotIn)(keyUsages, ['sign', 'verify'])) {
throw (0, _utils.lazyDOMException)(`Unsupported key usage for ${variant}`, 'SyntaxError');
}
const publicUsages = (0, _utils.getUsagesUnion)(keyUsages, 'verify');
const privateUsages = (0, _utils.getUsagesUnion)(keyUsages, 'sign');
if (privateUsages.length === 0) {
throw (0, _utils.lazyDOMException)('Usages cannot be empty', 'SyntaxError');
}
const slhdsa = new SlhDsa(variant);
await slhdsa.generateKeyPair();
const publicKeyData = slhdsa.getPublicKey();
const privateKeyData = slhdsa.getPrivateKey();
const pub = _classes.KeyObject.createKeyObject('public', publicKeyData, _utils.KFormatType.DER, _utils.KeyEncoding.SPKI);
const publicKey = new _classes.CryptoKey(pub, {
name: variant
}, publicUsages, true);
const priv = _classes.KeyObject.createKeyObject('private', privateKeyData, _utils.KFormatType.DER, _utils.KeyEncoding.PKCS8);
const privateKey = new _classes.CryptoKey(priv, {
name: variant
}, privateUsages, extractable);
return {
publicKey,
privateKey
};
}
//# sourceMappingURL=slhdsa.js.map