-
Notifications
You must be signed in to change notification settings - Fork 3
/
crypto.go
51 lines (44 loc) · 1.25 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
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package common
import (
"crypto/aes"
"crypto/cipher"
)
// CBCEncrypterPrivkey 使用钱包的password对私钥进行aes cbc加密,返回加密后的privkey
func CBCEncrypterPrivkey(password []byte, privkey []byte) []byte {
key := make([]byte, 32)
Encrypted := make([]byte, len(privkey))
if len(password) > 32 {
key = password[0:32]
} else {
copy(key, password)
}
block, err := aes.NewCipher(key)
if err != nil {
return nil
}
iv := key[:block.BlockSize()]
encrypter := cipher.NewCBCEncrypter(block, iv)
encrypter.CryptBlocks(Encrypted, privkey)
return Encrypted
}
// CBCDecrypterPrivkey 使用钱包的password对私钥进行aes cbc解密,返回解密后的privkey
func CBCDecrypterPrivkey(password []byte, privkey []byte) []byte {
key := make([]byte, 32)
if len(password) > 32 {
key = password[0:32]
} else {
copy(key, password)
}
block, err := aes.NewCipher(key)
if err != nil {
return nil
}
iv := key[:block.BlockSize()]
decryptered := make([]byte, len(privkey))
decrypter := cipher.NewCBCDecrypter(block, iv)
decrypter.CryptBlocks(decryptered, privkey)
return decryptered
}