forked from py60800/tuya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto.go
73 lines (69 loc) · 1.85 KB
/
crypto.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright 2019 py60800.
// Use of this source code is governed by Apache-2 licence
// license that can be found in the LICENSE file.
package tuya
import (
"crypto/aes"
"crypto/md5"
"encoding/base64"
"encoding/hex"
"errors"
//"log"
)
func md5Sign(b []byte, key []byte, version string) []byte {
h := md5.New()
h.Write([]byte("data="))
h.Write(b)
h.Write([]byte("||lpv=" + version + "||"))
h.Write(key)
hash := h.Sum(nil)
return []byte(hex.EncodeToString(hash[4:12]))
}
func aesEncrypt(data []byte, key []byte) ([]byte, error) {
block, err := aes.NewCipher([]byte(key))
if err != nil {
return nil, err
}
bs := block.BlockSize()
remain := len(data) % bs
if remain == 0 {
remain = bs
}
padd := make([]byte, bs-remain)
for i := range padd {
padd[i] = byte(bs - remain)
}
data = append(data, padd...)
ciphertext := make([]byte, len(data))
for i := 0; i < len(data); i = i + bs {
block.Encrypt(ciphertext[i:i+bs], data[i:i+bs])
}
return []byte(base64.StdEncoding.EncodeToString(ciphertext)), nil
}
func aesDecrypt(data []byte, key []byte) ([]byte, error) {
n := base64.StdEncoding.DecodedLen(len(data))
ciphertext := make([]byte, n)
nc, er1 := base64.StdEncoding.Decode(ciphertext, data)
if er1 != nil {
return []byte{}, er1
}
ciphertext = ciphertext[:nc]
block, er2 := aes.NewCipher([]byte(key))
if er2 != nil {
return []byte{}, er2
}
bs := block.BlockSize()
if nc%bs != 0 && nc < 16 {
return []byte{}, errors.New("Bad ciphertext len")
}
cleartext := make([]byte, nc)
for i := 0; i < nc; i = i + bs {
block.Decrypt(cleartext[i:i+bs], ciphertext[i:i+bs])
}
// remove padding
p := int(cleartext[nc-1])
if p < 0 || p > bs {
return []byte{}, errors.New("Bad padding")
}
return cleartext[:nc-p], nil
}