-
Notifications
You must be signed in to change notification settings - Fork 39
/
common.go
executable file
·72 lines (64 loc) · 1.56 KB
/
common.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
package account
import (
"crypto/aes"
"crypto/cipher"
"crypto/ecdsa"
"encoding/binary"
"github.com/ethereum/go-ethereum/crypto"
"io/ioutil"
"os"
"path/filepath"
)
// Uint32ToBytes trans uint32 num to bytes
func Uint32ToBytes(n uint32) []byte {
Bytes := make([]byte, 4)
binary.BigEndian.PutUint32(Bytes, n)
return Bytes
}
func writeTemporaryKeyFile(file string, content []byte) (string, error) {
const dirPerm = 0700
if err := os.MkdirAll(filepath.Dir(file), dirPerm); err != nil {
return "", err
}
f, err := ioutil.TempFile(filepath.Dir(file), "."+filepath.Base(file)+".tmp")
if err != nil {
return "", err
}
if _, err := f.Write(content); err != nil {
f.Close()
os.Remove(f.Name())
return "", err
}
f.Close()
return f.Name(), nil
}
func writeKeyFile(file string, content []byte) error {
name, err := writeTemporaryKeyFile(file, content)
if err != nil {
return err
}
return os.Rename(name, file)
}
func ensureInt(x interface{}) int {
res, ok := x.(int)
if !ok {
res = int(x.(float64))
}
return res
}
func aesCTRXOR(key, inText, iv []byte) ([]byte, error) {
aesBlock, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
stream := cipher.NewCTR(aesBlock, iv)
outText := make([]byte, len(inText))
stream.XORKeyStream(outText, inText)
return outText, err
}
// PublicKeyToRecipient publicKey to recipient
func PublicKeyToRecipient(p ecdsa.PublicKey) Recipient {
recipient := crypto.Keccak256(crypto.FromECDSAPub(&p)[1:])
recipientType := BytesToIdentityRecipient(recipient[(len(recipient) - RecipientLength):])
return recipientType
}