From ce03f2704417fc67090272072ad7022e5d649e94 Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Mon, 27 Apr 2015 07:42:52 +0100 Subject: [PATCH] refactor: use 'copy' instead of for loop for slice->array copy --- router/crypto.go | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/router/crypto.go b/router/crypto.go index c4a6dbfc..12b7735f 100644 --- a/router/crypto.go +++ b/router/crypto.go @@ -50,12 +50,8 @@ func DecryptPrefixNonce(ciphertxt []byte, secret *[32]byte) ([]byte, bool) { if len(ciphertxt) < secretbox.Overhead+24 { return nil, false } - // There is no way to nicely convert from a slice to an - // array. So have to used the following loop. var nonce [24]byte - for idx, e := range ciphertxt[0:24] { - nonce[idx] = e - } + copy(nonce[:], ciphertxt[0:24]) ciphertxt = ciphertxt[24:] return secretbox.Open(nil, ciphertxt, &nonce, secret) } @@ -91,11 +87,9 @@ func EncodeNonce(df bool) (*[24]byte, []byte, error) { func DecodeNonce(msg []byte) (bool, *[24]byte) { flags := uint16(msg[23]) df := 0 != (flags & 1) - nonce := [24]byte{} + var nonce [24]byte // upper bound is exclusive so this avoids copying the flags - for idx, elem := range msg[:23] { - nonce[idx] = elem - } + copy(nonce[:], msg[:23]) return df, &nonce }