expo-crypto-universal
Version:
Universal crypto implementation for Expo that works across all platforms including web
38 lines (37 loc) • 1.09 kB
JavaScript
class t {
/**
* Generates random bytes of specified size.
* @param size - The number of random bytes to generate.
* @returns Uint8Array containing random bytes.
*/
getRandomBytes(s) {
return this.getRandomValues(new Uint8Array(s));
}
/**
* Computes the SHA-2 hash of the given data asynchronously.
* @param bits - The number of bits to use (256, 384, or 512).
* @param data - The data to hash as a Uint8Array.
* @returns Promise resolving to a Uint8Array containing the SHA-2 hash.
* @throws Error if an unsupported bit length is provided.
*/
async sha2Async(s, n) {
switch (s) {
case 256:
return this.sha256Async(n);
case 384:
return this.sha384Async(n);
case 512:
return this.sha512Async(n);
default:
throw new Error(`Unsupported SHA-${s} hash`);
}
}
}
const r = () => {
var e;
return typeof window < "u" && typeof ((e = window == null ? void 0 : window.crypto) == null ? void 0 : e.getRandomValues) == "function";
};
export {
t as AbstractCryptoModule,
r as isWeb
};