mirror of
https://github.com/octoleo/restic.git
synced 2024-11-10 15:21:03 +00:00
crypto: Fix nonce test, make it faster
This commit is contained in:
parent
931e6ed2ac
commit
ba43c8bab5
@ -237,9 +237,9 @@ var ErrInvalidCiphertext = errors.New("invalid ciphertext, same slice used for p
|
|||||||
|
|
||||||
// validNonce checks that nonce is not all zero.
|
// validNonce checks that nonce is not all zero.
|
||||||
func validNonce(nonce []byte) bool {
|
func validNonce(nonce []byte) bool {
|
||||||
sum := 0
|
var sum byte
|
||||||
for b := range nonce {
|
for _, b := range nonce {
|
||||||
sum += b
|
sum |= b
|
||||||
}
|
}
|
||||||
return sum > 0
|
return sum > 0
|
||||||
}
|
}
|
||||||
|
@ -163,3 +163,30 @@ func TestCrypto(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNonceVadlid(t *testing.T) {
|
||||||
|
nonce := make([]byte, ivSize)
|
||||||
|
|
||||||
|
if validNonce(nonce) {
|
||||||
|
t.Error("null nonce detected as valid")
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < 100; i++ {
|
||||||
|
nonce = NewRandomNonce()
|
||||||
|
if !validNonce(nonce) {
|
||||||
|
t.Errorf("random nonce not detected as valid: %02x", nonce)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkNonceValid(b *testing.B) {
|
||||||
|
nonce := NewRandomNonce()
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
if !validNonce(nonce) {
|
||||||
|
b.Fatal("nonce is invalid")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user