diff --git a/js/src/config.ts b/js/src/config.ts index a533892..a771601 100644 --- a/js/src/config.ts +++ b/js/src/config.ts @@ -1,7 +1,9 @@ import { ChaCha20Poly1305 } from '@stablelib/chacha20poly1305' -import { subtle } from 'crypto' import { AlgorithmConfig, EncryptionAlgorithm } from './types' import { bitsToUint8Array, bitsToUintArray, toUint8Array, toUintArray, uint8ArrayToBits, uintArrayToBits } from './utils' +import { webcrypto } from './webcrypto' + +const { subtle } = webcrypto // commit hash for this repo export const GIT_COMMIT_HASH = 'c9df4f9eef53566536a0afec5444b9544630fa0c' diff --git a/js/src/webcrypto.ts b/js/src/webcrypto.ts new file mode 100644 index 0000000..bb87062 --- /dev/null +++ b/js/src/webcrypto.ts @@ -0,0 +1,14 @@ +export const webcrypto = (() => { + // if we're in node, we need to use + // webcrypto provided by the crypto module + if(typeof window !== 'undefined') { + return window.crypto + } + + if(typeof self !== 'undefined' && self.crypto) { + return self.crypto + } + + const { webcrypto } = require('crypto') + return webcrypto as Crypto +})() \ No newline at end of file