one
Version:
One is a React framework for web, iOS, and Android apps.
62 lines (59 loc) • 2.28 kB
JavaScript
import { createAesApi } from "./aes.native.js";
import { installDigestPolyfill } from "./digest.native.js";
import { createDigestApi } from "./digest-api.native.js";
import { NitroModules } from "react-native-nitro-modules";
import { installCryptoPolyfill } from "./random.native.js";
var hybrid;
function native() {
return hybrid !== null && hybrid !== void 0 ? hybrid : hybrid = NitroModules.createHybridObject("OneCrypto");
}
var nativeSource = {
fill(bytes) {
native().fillRandomBytes(bytes.buffer, bytes.byteOffset, bytes.byteLength);
},
randomUUID: function randomUUID() {
return native().randomUUID();
}
};
function required() {
if (!NitroModules.hasHybridObject("OneCrypto")) {
throw new Error("Crypto needs a native build that includes one");
}
return native();
}
var aesEngine = {
async seal(key, iv, plaintext, additionalData, tagLength) {
var sealed = await required().aesEncrypt(key.buffer, iv.buffer, plaintext.buffer, additionalData === null || additionalData === void 0 ? void 0 : additionalData.buffer, tagLength);
return new Uint8Array(sealed);
},
async open(key, iv, ciphertextWithTag, additionalData, tagLength) {
var opened = await required().aesDecrypt(key.buffer, iv.buffer, ciphertextWithTag.buffer, additionalData === null || additionalData === void 0 ? void 0 : additionalData.buffer, tagLength);
return new Uint8Array(opened);
},
random(bytes) {
required().fillRandomBytes(bytes.buffer, bytes.byteOffset, bytes.byteLength);
}
};
var aes = createAesApi(aesEngine);
var digestApi = createDigestApi(function(algorithm, buffer, offset, length) {
return required().digest(algorithm, buffer, offset, length);
});
var AESEncryptionKey = aes.AESEncryptionKey;
var AESSealedData = aes.AESSealedData;
var Crypto = Object.freeze({
digest: digestApi.digest,
digestString: digestApi.digestString,
aesEncrypt: aes.aesEncrypt,
aesDecrypt: aes.aesDecrypt
});
function installCrypto() {
if (!NitroModules.hasHybridObject("OneCrypto")) {
return;
}
installCryptoPolyfill(nativeSource);
installDigestPolyfill(function(algorithm, buffer, offset, length) {
return native().digest(algorithm, buffer, offset, length);
});
}
export { AESEncryptionKey, AESSealedData, Crypto, installCrypto };
//# sourceMappingURL=index.native.js.map