-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathutil.go
More file actions
45 lines (38 loc) · 798 Bytes
/
util.go
File metadata and controls
45 lines (38 loc) · 798 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
33
34
35
36
37
38
39
40
41
42
43
44
45
package gomat
import (
"bytes"
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
"encoding/binary"
"io"
"golang.org/x/crypto/hkdf"
)
func CreateRandomBytes(n int) []byte {
out := make([]byte, n)
rand.Read(out)
return out
}
func id_to_bytes(id uint64) []byte {
var b bytes.Buffer
binary.Write(&b, binary.LittleEndian, id)
return b.Bytes()
}
func hmac_sha256_enc(in []byte, key []byte) []byte {
mac := hmac.New(sha256.New, key)
mac.Write(in)
return mac.Sum(nil)
}
func sha256_enc(in []byte) []byte {
s := sha256.New()
s.Write(in)
return s.Sum(nil)
}
func hkdf_sha256(secret, salt, info []byte, size int) []byte {
engine := hkdf.New(sha256.New, secret, salt, info)
key := make([]byte, size)
if _, err := io.ReadFull(engine, key); err != nil {
return []byte{}
}
return key
}