diff --git a/README.md b/README.md index c0aec0f..531abb5 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,7 @@ import { crypto_sign_SEEDBYTES, crypto_sign_verify_detached, from_base64, + memzero, randombytes_buf, randombytes_uniform, to_base64, diff --git a/example/src/components/TestResults.tsx b/example/src/components/TestResults.tsx index 806ca22..4ff9dc2 100644 --- a/example/src/components/TestResults.tsx +++ b/example/src/components/TestResults.tsx @@ -27,6 +27,7 @@ import '../tests/crypto_sign_keypair_test'; import '../tests/crypto_sign_seed_keypair_test'; import '../tests/crypto_sign_verify_detached_test'; import '../tests/from_base64_test'; +import '../tests/memzero_test'; import '../tests/randombytes_buf_test'; import '../tests/randombytes_uniform_test'; import '../tests/to_base64_test'; diff --git a/example/src/tests/memzero_test.ts b/example/src/tests/memzero_test.ts new file mode 100644 index 0000000..b566816 --- /dev/null +++ b/example/src/tests/memzero_test.ts @@ -0,0 +1,10 @@ +import { memzero, to_hex } from 'react-native-libsodium'; +import { expect, test } from '../utils/testRunner'; + +test('memzero', () => { + let buf = new Uint8Array([222, 173, 190, 239]); + memzero(buf); + expect(to_hex(buf)).toBe('00000000'); + + expect(() => memzero([0, 1, 2, 3] as unknown as Uint8Array)).toThrow(); +}); diff --git a/src/lib.native.ts b/src/lib.native.ts index 134678d..ba1b714 100644 --- a/src/lib.native.ts +++ b/src/lib.native.ts @@ -251,6 +251,15 @@ export function to_hex(input: string | Uint8Array): string { return global.jsi_to_hex(inputParam); } +export function memzero(bytes: Uint8Array): void { + if (!(bytes instanceof Uint8Array)) { + throw new TypeError('Only Uint8Array instances can be wiped'); + } + for (let i = 0; i < bytes.length; i++) { + bytes[i] = 0; + } +} + export function randombytes_buf( length: number, outputFormat?: Uint8ArrayOutputFormat | null @@ -889,6 +898,7 @@ export default { crypto_sign_keypair, crypto_sign_verify_detached, from_base64, + memzero, randombytes_buf, randombytes_uniform, ready,