1
0
mirror of https://github.com/fumiama/blake2b-simd.git synced 2026-06-05 02:00:26 +08:00
Files
blake2b-simd/binary_test.go
2025-02-28 13:59:19 +09:00

62 lines
1.4 KiB
Go

/*
* Copyright (c) 2025 Fumiama Minamoto.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package blake2b
import (
"bytes"
"crypto/rand"
"encoding"
"encoding/hex"
"testing"
)
func TestBinaryMarshalUnmarshal256(t *testing.T) {
buf := make([]byte, 16384)
rand.Read(buf)
var (
exp [32]byte
got [32]byte
)
for i := 0; i < 16384; i++ {
h := New256()
_, err := h.Write(buf[:i])
if err != nil {
t.Fatal(err)
}
data, err := h.(encoding.BinaryMarshaler).MarshalBinary()
if err != nil {
t.Fatal(err)
}
newh := New256()
err = newh.(encoding.BinaryUnmarshaler).UnmarshalBinary(data)
if err != nil {
t.Fatal(err)
}
_, err = h.Write(buf[i:])
if err != nil {
t.Fatal(err)
}
_, err = newh.Write(buf[i:])
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(h.Sum(exp[:0]), newh.Sum(got[:0])) {
t.Fatal("Expect", hex.EncodeToString(exp[:]), "but got", hex.EncodeToString(got[:]))
}
}
}