-
-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathgenerateChallenge.ts
More file actions
29 lines (25 loc) · 908 Bytes
/
generateChallenge.ts
File metadata and controls
29 lines (25 loc) · 908 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { isoCrypto } from './iso/index.ts';
import type { Uint8Array_ } from '../types/index.ts';
/**
* Generate a suitably random value to be used as an attestation or assertion challenge
*/
export async function generateChallenge(): Promise<Uint8Array_> {
/**
* WebAuthn spec says that 16 bytes is a good minimum:
*
* "In order to prevent replay attacks, the challenges MUST contain enough entropy to make
* guessing them infeasible. Challenges SHOULD therefore be at least 16 bytes long."
*
* Just in case, let's double it
*/
const challenge = new Uint8Array(32);
await isoCrypto.getRandomValues(challenge);
return _generateChallengeInternals.stubThis(challenge);
}
/**
* Make it possible to stub the return value during testing
* @ignore Don't include this in docs output
*/
export const _generateChallengeInternals = {
stubThis: (value: Uint8Array_) => value,
};