-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert.ts
More file actions
64 lines (61 loc) · 2.13 KB
/
convert.ts
File metadata and controls
64 lines (61 loc) · 2.13 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* @fileoverview SSRI ↔ hex digest conversion helpers — `hexToSsri`
* wraps a hex digest in `<algorithm>-<base64>` form, `ssriToHex`
* decodes the base64 half back to hex.
*/
import { BufferFrom, BufferPrototypeToString } from '../primordials/buffer'
import { ErrorCtor } from '../primordials/error'
/**
* Convert hex format hash to SSRI format.
*
* Takes a hash in hex format and converts it to SSRI format with the specified
* algorithm prefix (defaults to sha256).
*
* @param hex - Hash in hex format
* @param algorithm - Hash algorithm (default: 'sha256')
* @returns SSRI format hash (algorithm-base64)
* @throws Error if hex format is invalid
*
* @example
* ```typescript
* const ssri = hexToSsri('76682a9fc3bbe62975176e2541f39a8168877d828d5cad8b56461fc36ac2b856')
* // Returns: 'sha256-dmgqn8O75il1F24lQfOagWiHfYKNXK2LVkYfw2rCuFY='
* ```
*/
/*@__NO_SIDE_EFFECTS__*/
export function hexToSsri(hex: string, algorithm = 'sha256'): string {
if (!/^[a-f0-9]+$/i.test(hex)) {
throw new ErrorCtor(`Invalid hex format: ${hex}`)
}
// Convert hex to base64.
const buffer = BufferFrom!(hex, 'hex')
const base64Hash = BufferPrototypeToString!(buffer, 'base64')
return `${algorithm}-${base64Hash}`
}
/**
* Convert SSRI format hash to hex format.
*
* Takes a hash in SSRI format (e.g., "sha256-base64hash") and converts it to
* standard hex format (e.g., "hexstring").
*
* @param ssri - Hash in SSRI format (algorithm-base64)
* @returns Hex string representation of the hash
* @throws Error if SSRI format is invalid
*
* @example
* ```typescript
* const hex = ssriToHex('sha256-dmgqn8O75il1F24lQfOagWiHfYKNXK2LVkYfw2rCuFY=')
* // Returns: '76682a9fc3bbe62975176e2541f39a8168877d828d5cad8b56461fc36ac2b856'
* ```
*/
/*@__NO_SIDE_EFFECTS__*/
export function ssriToHex(ssri: string): string {
const match = /^([a-z0-9]+)-([A-Za-z0-9+/]+=*)$/i.exec(ssri)
if (!match || !match[2] || match[2].length < 2) {
throw new ErrorCtor(`Invalid SSRI format: ${ssri}`)
}
const base64Hash = match[2]
// Convert base64 to hex.
const buffer = BufferFrom!(base64Hash, 'base64')
return BufferPrototypeToString!(buffer, 'hex')
}