-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathnonce.ts
More file actions
32 lines (27 loc) · 778 Bytes
/
nonce.ts
File metadata and controls
32 lines (27 loc) · 778 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
30
31
32
import type { bytes, uint64 } from './@types/basic'
/**
* The nonce is an uint that's increased over time.
* Maintaining different representations help improve performance.
*/
export class Nonce {
private n: uint64
private readonly bytes: bytes
private readonly view: DataView
constructor (n: uint64) {
this.n = n
this.bytes = new Uint8Array(12)
this.view = new DataView(this.bytes.buffer, this.bytes.byteOffset, this.bytes.byteLength)
this.view.setUint32(4, n, true)
}
increase (): void {
this.n++
// Even though we're treating the nonce as 8 bytes, RFC7539 specifies 12 bytes for a nonce.
this.view.setUint32(4, this.n, true)
}
getBytes (): bytes {
return this.bytes
}
getUint64 (): uint64 {
return this.n
}
}