Page MenuHomePhorge

aes-crypto-module.js
No OneTemporary

Size
1 KB
Referenced Files
None
Subscribers
None

aes-crypto-module.js

// @flow
import { requireNativeModule } from 'expo-modules-core';
import invariant from 'invariant';
const KEY_SIZE = 32; // bytes
const IV_LENGTH = 12; // bytes, IV - unique Initialization Vector (nonce)
const TAG_LENGTH = 16; // bytes - GCM auth tag
const AESCryptoModule: {
+generateKey: (destination: Uint8Array) => void,
+encrypt: (
key: Uint8Array,
data: Uint8Array,
destination: Uint8Array,
) => void,
+decrypt: (
key: Uint8Array,
data: Uint8Array,
destination: Uint8Array,
) => void,
} = requireNativeModule('AESCrypto');
export function generateKey(): Uint8Array {
const keyBuffer = new Uint8Array(KEY_SIZE);
AESCryptoModule.generateKey(keyBuffer);
return keyBuffer;
}
export function encrypt(key: Uint8Array, data: Uint8Array): Uint8Array {
const sealedDataBuffer = new Uint8Array(data.length + IV_LENGTH + TAG_LENGTH);
AESCryptoModule.encrypt(key, data, sealedDataBuffer);
return sealedDataBuffer;
}
export function decrypt(key: Uint8Array, data: Uint8Array): Uint8Array {
invariant(data.length >= IV_LENGTH + TAG_LENGTH, 'Invalid data length');
const plaintextBuffer = new Uint8Array(data.length - IV_LENGTH - TAG_LENGTH);
AESCryptoModule.decrypt(key, data, plaintextBuffer);
return plaintextBuffer;
}

File Metadata

Mime Type
text/x-java
Expires
Sun, Dec 7, 4:26 PM (1 d, 1 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5829724
Default Alt Text
aes-crypto-module.js (1 KB)

Event Timeline