Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/cryptography/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Initial release ([#10282](https://github.com/MetaMask/core/pull/10282), [#10431](https://github.com/MetaMask/core/pull/10431), [#10403](https://github.com/MetaMask/core/pull/10403), [#10468](https://github.com/MetaMask/core/pull/10468), [#10503](https://github.com/MetaMask/core/pull/10503))
- Initial release ([#10282](https://github.com/MetaMask/core/pull/10282), [#10431](https://github.com/MetaMask/core/pull/10431), [#10403](https://github.com/MetaMask/core/pull/10403), [#10468](https://github.com/MetaMask/core/pull/10468), [#10503](https://github.com/MetaMask/core/pull/10503), [#10563](https://github.com/MetaMask/core/pull/10563))
- Add `sha256`, `sha384`, and `sha512` functions for computing SHA digests
- Add `hmacSha256`, `hmacSha384`, and `hmacSha512` functions for computing HMAC digests
- Add `pbkdf2Sha256`, `pbkdf2Sha384`, and `pbkdf2Sha512` functions for key derivation
- Add `hkdfSha256`, `hkdfSha384`, and `hkdfSha512` functions for key derivation
- Add `getPublicKey` and `getSharedSecret` functions for X25519 key derivation exported via `@metamask/cryptography/x25519`
- Add `getRandomBytes` function for generating cryptographically secure random bytes

[Unreleased]: https://github.com/MetaMask/core/
1 change: 1 addition & 0 deletions packages/cryptography/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './hkdf.js';
export * from './hmac.js';
export * from './pbkdf2.js';
export * from './random.js';
export * from './sha.js';
export type * from './types.js';
31 changes: 31 additions & 0 deletions packages/cryptography/src/random.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { getRandomBytes } from './random.js';

describe('getRandomBytes', () => {
it('returns a Uint8Array of the requested length', () => {
const bytes = getRandomBytes(32);

expect(bytes).toBeInstanceOf(Uint8Array);
expect(bytes).toHaveLength(32);
});

it('fills the array using `crypto.getRandomValues`', () => {
const spy = jest
.spyOn(globalThis.crypto, 'getRandomValues')
.mockImplementation((array) => {
(array as Uint8Array).fill(0xab);
return array;
});

const bytes = getRandomBytes(4);

expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith(expect.any(Uint8Array));
expect(bytes).toStrictEqual(new Uint8Array([0xab, 0xab, 0xab, 0xab]));
});

it('throws if the length is zero', () => {
expect(() => getRandomBytes(0)).toThrow(
'Invalid length: Length must be greater than 0.',
);
});
});
13 changes: 13 additions & 0 deletions packages/cryptography/src/random.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Generate random bytes using the platform's cryptographically secure pseudo-random number generator (CSPRNG).
*
* @param length - The number of random bytes to generate.
* @returns An `Uint8Array` of the provided length with random bytes.
* @throws If `length` is not greater than 0.
*/
export function getRandomBytes(length: number): Uint8Array {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe validate length > 0?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure!

@FrederikBolding FrederikBolding Sep 29, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (length <= 0) {
throw new Error('Invalid length: Length must be greater than 0.');
}
return globalThis.crypto.getRandomValues(new Uint8Array(length));
}
Loading