/
key.go
52 lines (44 loc) · 918 Bytes
/
key.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
49
50
51
52
/*
* Copyright (c) 2017 ZAP Québec
* Copyright (c) 2020 Gaetan Crahay
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
*/
package inform
import (
"bytes"
"encoding/hex"
"fmt"
)
// Key management
type Key []byte
func (k Key) IsValid() bool {
return len(k) == len(DefaultKey)
}
func (k Key) IsDefault() bool {
return bytes.Equal(k, DefaultKey)
}
func (k Key) String() string {
return hex.EncodeToString(k)
}
func KeyFromString(keyString string) (Key, error) {
keyBytes, err := hex.DecodeString(keyString)
if err != nil {
return nil, err
}
key := Key(keyBytes)
if !key.IsValid() {
return nil, fmt.Errorf("invalid key length")
}
return key, nil
}
var (
DefaultKey = Key([]byte{
0xba, 0x86, 0xf2, 0xbb,
0xe1, 0x07, 0xc7, 0xc5,
0x7e, 0xb5, 0xf2, 0x69,
0x07, 0x75, 0xc7, 0x12,
})
)