mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-09-01 10:54:02 +03:00
Small refactorings of rpc package
This commit is contained in:
@@ -6,36 +6,33 @@ import (
|
||||
"github.com/juju/errors"
|
||||
)
|
||||
|
||||
const rpcNonceResponseLength = rpcNonceRequestLength
|
||||
|
||||
type RPCNonceResponse struct {
|
||||
RPCNonceRequest
|
||||
|
||||
RPCType [rpcNonceTagLength]byte
|
||||
Crypto [rpcNonceCryptoAESLength]byte
|
||||
RPCType []byte
|
||||
Crypto []byte
|
||||
}
|
||||
|
||||
func (r *RPCNonceResponse) Bytes() []byte {
|
||||
buf := &bytes.Buffer{}
|
||||
buf.Grow(rpcNonceResponseLength)
|
||||
|
||||
buf.Write(r.RPCType[:])
|
||||
buf.Write(r.KeySelector[:])
|
||||
buf.Write(r.Crypto[:])
|
||||
buf.Write(r.CryptoTS[:])
|
||||
buf.Write(r.Nonce[:])
|
||||
buf.Write(r.RPCType)
|
||||
buf.Write(r.KeySelector)
|
||||
buf.Write(r.Crypto)
|
||||
buf.Write(r.CryptoTS)
|
||||
buf.Write(r.Nonce)
|
||||
|
||||
return buf.Bytes()
|
||||
}
|
||||
|
||||
func (r *RPCNonceResponse) Valid(req *RPCNonceRequest) error {
|
||||
if r.RPCType != rpcNonceTag {
|
||||
if !bytes.Equal(r.RPCType, RPCTagNonce) {
|
||||
return errors.New("Unexpected RPC type")
|
||||
}
|
||||
if r.Crypto != rpcNonceCryptoAESTag {
|
||||
if !bytes.Equal(r.Crypto, RPCNonceCryptoAES) {
|
||||
return errors.New("Unexpected crypto type")
|
||||
}
|
||||
if r.KeySelector != req.KeySelector {
|
||||
if !bytes.Equal(r.KeySelector, req.KeySelector) {
|
||||
return errors.New("Unexpected key selector")
|
||||
}
|
||||
|
||||
@@ -43,16 +40,17 @@ func (r *RPCNonceResponse) Valid(req *RPCNonceRequest) error {
|
||||
}
|
||||
|
||||
func NewRPCNonceResponse(data []byte) (*RPCNonceResponse, error) {
|
||||
if len(data) != rpcNonceResponseLength {
|
||||
if len(data) != 32 {
|
||||
return nil, errors.New("Unexpected message length")
|
||||
}
|
||||
|
||||
resp := RPCNonceResponse{}
|
||||
copy(resp.RPCType[:], data[:4])
|
||||
copy(resp.KeySelector[:], data[4:8])
|
||||
copy(resp.Crypto[:], data[8:12])
|
||||
copy(resp.CryptoTS[:], data[12:16])
|
||||
copy(resp.Nonce[:], data[16:])
|
||||
|
||||
return &resp, nil
|
||||
return &RPCNonceResponse{
|
||||
RPCNonceRequest: RPCNonceRequest{
|
||||
KeySelector: data[4:8],
|
||||
CryptoTS: data[12:16],
|
||||
Nonce: data[16:],
|
||||
},
|
||||
RPCType: data[:4],
|
||||
Crypto: data[8:12],
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user