This repository was archived by the owner on Feb 17, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhelpers.odin
More file actions
96 lines (86 loc) · 1.9 KB
/
helpers.odin
File metadata and controls
96 lines (86 loc) · 1.9 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import "core:crypto/hash"
import "core:fmt"
import "core:log"
import "core:os"
import "core:strings"
panic_fn :: proc(arg: any)
env_to_log_level :: proc() -> log.Level {
s := os.get_env_alloc("ODIN_LOG", context.allocator)
defer delete(s)
switch s {
case "debug":
return .Debug
case "info":
return .Info
case "warn", "warning":
return .Warning
case "error":
return .Error
case "fatal":
return .Fatal
}
return .Info
}
hash_name_to_algorithm :: proc(alg_str: string) -> (hash.Algorithm, bool) {
alg_enums := [][hash.Algorithm]string {
hash.ALGORITHM_NAMES,
// The HMAC test vectors omit `-`.
#partial [hash.Algorithm]string {
.SHA224 = "SHA224",
.SHA256 = "SHA256",
.SHA384 = "SHA384",
.SHA512 = "SHA512",
.SHA512_256 = "SHA512/256",
.Insecure_SHA1 = "SHA1",
},
}
for &e in alg_enums {
for n, alg in e {
if n == alg_str {
return alg, true
}
}
}
return .Invalid, false
}
MAC_ALGORITHM :: enum {
Invalid,
HMAC,
KMAC128,
KMAC256,
SIPHASH_1_3,
SIPHASH_2_4,
SIPHASH_4_8,
}
mac_algorithm :: proc(alg_str: string) -> (MAC_ALGORITHM, hash.Algorithm, string, bool) {
PREFIX_HMAC :: "HMAC"
if strings.has_prefix(alg_str, PREFIX_HMAC) {
alg_str_ := strings.trim_prefix(alg_str, PREFIX_HMAC)
alg, ok := hash_name_to_algorithm(alg_str_)
alg_str_ = fmt.aprintf("hmac/%s", strings.to_lower(alg_str_))
return .HMAC, alg, alg_str_, ok
}
ALG_KMAC128 :: "KMAC128"
ALG_KMAC256 :: "KMAC256"
ALG_SIPHASH_1_3 :: "SipHash-1-3"
ALG_SIPHASH_2_4 :: "SipHash-2-4"
ALG_SIPHASH_4_8 :: "SipHash-4-8"
mac_alg := MAC_ALGORITHM.Invalid
ok := true
switch alg_str {
case ALG_KMAC128:
mac_alg = .KMAC128
case ALG_KMAC256:
mac_alg = .KMAC256
case ALG_SIPHASH_1_3:
mac_alg = .SIPHASH_1_3
case ALG_SIPHASH_2_4:
mac_alg = .SIPHASH_2_4
case ALG_SIPHASH_4_8:
mac_alg = .SIPHASH_4_8
case:
ok = false
}
return mac_alg, .Invalid, strings.to_lower(alg_str), ok
}