-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkey_simple.go
48 lines (39 loc) · 948 Bytes
/
key_simple.go
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
package gocrypto
import (
"crypto/rand"
"encoding/base64"
"encoding/hex"
)
type SimpleLength int
const (
Simple16 SimpleLength = 16
Simple24 SimpleLength = 24
Simple32 SimpleLength = 32
)
// NewSimpleKey generate new random key.
func NewSimpleKey(length SimpleLength) (SimpleKey, error) {
key := make(SimpleKey, length)
_, err := rand.Read(key)
if err != nil {
return nil, err
}
return key, nil
}
// SimpleKey implementation of the Key interface for simple keys.
type SimpleKey []byte
// Bytes returns the key as a byte slice.
func (key SimpleKey) Bytes() []byte {
return key
}
// Hex returns the key as a hex string.
func (key SimpleKey) Hex() string {
return hex.EncodeToString(key)
}
// Base64 returns the key as a base64 string.
func (key SimpleKey) Base64() string {
return base64.StdEncoding.EncodeToString(key)
}
// String implement fmt.Stringer interface.
func (key SimpleKey) String() string {
return key.Hex()
}