-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash_example_test.go
More file actions
66 lines (59 loc) · 2.23 KB
/
hash_example_test.go
File metadata and controls
66 lines (59 loc) · 2.23 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
package core_test
import . "dappco.re/go"
// ExampleSHA256 hashes a byte payload through `SHA256` for session-token cryptography.
// Callers stay on the core wrapper surface while receiving fixed-size digests or Result
// values.
func ExampleSHA256() {
sum := SHA256([]byte("hello"))
Println(HexEncode(sum[:]))
// Output: 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
}
// ExampleSHA256Hex hashes bytes and formats hex through `SHA256Hex` for session-token
// cryptography. Callers stay on the core wrapper surface while receiving fixed-size
// digests or Result values.
func ExampleSHA256Hex() {
Println(SHA256Hex([]byte("hello")))
// Output: 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
}
// ExampleSHA256String hashes a string payload through `SHA256String` for session-token
// cryptography. Callers stay on the core wrapper surface while receiving fixed-size
// digests or Result values.
func ExampleSHA256String() {
sum := SHA256String("hello")
Println(HexEncode(sum[:]))
// Output: 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
}
// ExampleSHA256HexString hashes a string and formats hex through `SHA256HexString` for
// session-token cryptography. Callers stay on the core wrapper surface while receiving
// fixed-size digests or Result values.
func ExampleSHA256HexString() {
Println(SHA256HexString("hello"))
// Output: 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
}
// ExampleHMAC signs a payload with a shared secret for session-token validation. Callers
// stay on the core wrapper surface while receiving fixed-size digests or Result values.
func ExampleHMAC() {
r := HMAC("sha256", []byte("secret"), []byte("payload"))
if r.OK {
digest := r.Value.([]byte)
Println(len(digest))
Println(HexEncode(digest)[:8])
}
// Output:
// 32
// b82fcb79
}
// ExampleHKDF derives a session key from a master secret, salt, context label, and output
// length. Callers stay on the core wrapper surface while receiving fixed-size digests or
// Result values.
func ExampleHKDF() {
r := HKDF("sha256", []byte("secret"), []byte("salt"), []byte("session"), 32)
if r.OK {
key := r.Value.([]byte)
Println(len(key))
Println(HexEncode(key)[:8])
}
// Output:
// 32
// 2baf2709
}