-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate.ts
More file actions
46 lines (44 loc) · 1.37 KB
/
validate.ts
File metadata and controls
46 lines (44 loc) · 1.37 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
/**
* @fileoverview SSRI/hex format validators — predicates that report
* whether a string looks like a hex digest or an SSRI integrity
* string. Validation is purely lexical; the predicates do not verify
* algorithm strength or digest length.
*/
/**
* Check if a string is valid hex format.
*
* Validates that a string contains only hexadecimal characters (0-9, a-f).
* Does not verify hash length or algorithm.
*
* @param value - String to validate
* @returns True if string is valid hex format
*
* @example
* ```typescript
* isValidHex('76682a9fc3bbe62975176e2541f39a8168877d828d5cad8b56461fc36ac2b856') // true
* isValidHex('sha256-dmgqn8O75il1F24lQfOagWiHfYKNXK2LVkYfw2rCuFY=') // false
* ```
*/
/*@__NO_SIDE_EFFECTS__*/
export function isValidHex(value: string): boolean {
return /^[a-f0-9]+$/i.test(value)
}
/**
* Check if a string is valid SSRI format.
*
* Validates that a string matches the SSRI format pattern (algorithm-base64).
* Does not verify that the base64 encoding is valid.
*
* @param value - String to validate
* @returns True if string matches SSRI format
*
* @example
* ```typescript
* isValidSsri('sha256-dmgqn8O75il1F24lQfOagWiHfYKNXK2LVkYfw2rCuFY=') // true
* isValidSsri('76682a9f...') // false
* ```
*/
/*@__NO_SIDE_EFFECTS__*/
export function isValidSsri(value: string): boolean {
return /^[a-z0-9]+-[A-Za-z0-9+/]{2,}=*$/i.test(value)
}