Small refactorings of rpc package

This commit is contained in:
9seconds
2018-07-05 10:37:05 +03:00
parent a7eb29cc94
commit 0738631ef8
6 changed files with 91 additions and 128 deletions
+14 -30
View File
@@ -9,52 +9,36 @@ import (
"github.com/juju/errors"
)
const (
rpcNonceLength = 16
rpcNonceKeySelectorLength = 4
rpcNonceCryptoTSLength = 4
rpcNonceTagLength = 4
rpcNonceCryptoAESLength = 4
rpcNonceRequestLength = rpcNonceTagLength + rpcNonceKeySelectorLength + rpcNonceCryptoAESLength + rpcNonceCryptoTSLength + rpcNonceLength
)
var (
rpcNonceTag = [rpcNonceTagLength]byte{0xaa, 0x87, 0xcb, 0x7a}
rpcNonceCryptoAESTag = [rpcNonceCryptoAESLength]byte{0x01, 0x00, 0x00, 0x00}
)
type RPCNonceRequest struct {
KeySelector [rpcNonceKeySelectorLength]byte
CryptoTS [rpcNonceCryptoTSLength]byte
Nonce [rpcNonceLength]byte
KeySelector []byte
CryptoTS []byte
Nonce []byte
}
func (r *RPCNonceRequest) Bytes() []byte {
buf := &bytes.Buffer{}
buf.Grow(rpcNonceRequestLength)
buf.Write(rpcNonceTag[:])
buf.Write(r.KeySelector[:])
buf.Write(rpcNonceCryptoAESTag[:])
buf.Write(r.CryptoTS[:])
buf.Write(r.Nonce[:])
buf.Write(RPCTagNonce)
buf.Write(r.KeySelector)
buf.Write(RPCNonceCryptoAES)
buf.Write(r.CryptoTS)
buf.Write(r.Nonce)
return buf.Bytes()
}
func NewRPCNonceRequest(proxySecret []byte) (*RPCNonceRequest, error) {
var nonce [rpcNonceLength]byte
var keySelector [rpcNonceKeySelectorLength]byte
var cryptoTS [rpcNonceCryptoTSLength]byte
nonce := make([]byte, 16)
keySelector := make([]byte, 4)
cryptoTS := make([]byte, 4)
if _, err := rand.Read(nonce[:]); err != nil {
if _, err := rand.Read(nonce); err != nil {
return nil, errors.Annotate(err, "Cannot generate nonce")
}
copy(keySelector[:], proxySecret)
copy(keySelector, proxySecret)
timestamp := time.Now().Truncate(time.Second).Unix() % 4294967296 // 256 ^ 4 - do not know how to name
binary.LittleEndian.PutUint32(cryptoTS[:], uint32(timestamp))
binary.LittleEndian.PutUint32(cryptoTS, uint32(timestamp))
return &RPCNonceRequest{
KeySelector: keySelector,