crypto: a few extra tests (#7795)

* crypto: keccak256 tests
* crypto: ecies decrypt test
This commit is contained in:
battlmonstr 2023-06-29 14:51:30 +02:00 committed by GitHub
parent affef546b1
commit ecc1514321
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 0 deletions

View File

@ -20,6 +20,7 @@ import (
"bytes"
"crypto/ecdsa"
"encoding/hex"
"golang.org/x/crypto/sha3"
"os"
"reflect"
"testing"
@ -52,6 +53,33 @@ func TestKeccak256Hasher(t *testing.T) {
checkhash(t, "Sha3-256-array", func(in []byte) []byte { h := HashData(hasher, in); return h[:] }, msg, exp)
}
func TestKeccak256HasherNew(t *testing.T) {
msg := []byte("abc")
exp, _ := hex.DecodeString("3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532")
hasher := sha3.New256()
hasher.Write(msg)
var h libcommon.Hash
if !bytes.Equal(exp, hasher.Sum(h[:0])) {
t.Fatalf("hash %s mismatch: want: %x have: %x", "new", exp, h[:])
}
}
func TestKeccak256HasherMulti(t *testing.T) {
exp1, _ := hex.DecodeString("d341f310fa772d37e6966b84b37ad760811d784729b641630f6a03f729e1e20e")
exp2, _ := hex.DecodeString("6de9c0166df098306abb98b112c0834c29eedee6fcba804c7c4f4568204c9d81")
hasher := NewKeccakState()
d1, _ := hex.DecodeString("1234")
hasher.Write(d1)
d2, _ := hex.DecodeString("cafe")
hasher.Write(d2)
d3, _ := hex.DecodeString("babe")
hasher.Write(d3)
checkhash(t, "multi1", func(in []byte) []byte { var h libcommon.Hash; return hasher.Sum(h[:0]) }, []byte{}, exp1)
d4, _ := hex.DecodeString("5678")
hasher.Write(d4)
checkhash(t, "multi2", func(in []byte) []byte { var h libcommon.Hash; return hasher.Sum(h[:0]) }, []byte{}, exp2)
}
func TestToECDSAErrors(t *testing.T) {
if _, err := HexToECDSA("0000000000000000000000000000000000000000000000000000000000000000"); err == nil {
t.Fatal("HexToECDSA should've returned error")

View File

@ -440,3 +440,13 @@ func decode(s string) []byte {
}
return bytes
}
func TestDecrypt(t *testing.T) {
data, _ := hex.DecodeString("043c8d19a2957e1f259cf325ad4c7f60a94bead921c7cedc135600511d51ee1d7f44d72fde3b9c9506dd3e6c69f4c10c910ea4257e42cd4335531cb2add1aed3b47e568f1473487279fdac238aa323409df92235a13d8a9036ac8d2ad3968c5f0483cd7a5fd6a441e520870644d3c61a630229b01f3e19fbd25e751ec9cfa5782abcd48a5ee406742d20a329e005761316f6963b0ec4b50f2ec3bbb022227961893a51ae568094267f27babeae3b452de67cd084fb5d03c635d7cebba86f8814b469ead9dad2504b79ca6e08e8f1db59747470054c61638000687b04a83af75111e196d253ef42697da2dd11c2bf67796b8f273a5161d7fdcfbc77332f3e0872dede7c33d6671b0b7fc7bf62db549123b0dfa66a2d76dd921faf9de35522863c8b7bc3d1a37af2d1b7f347bfdcf29b3fb7b038b86e22bd3b1a8e5b2520c52ea4ac1ce968672325bc1332b0966d2c5280b6980431e86792a485e5402aada661c6c848635d0fee662dcaa117249d346f875ffe7d85de9f6fa146d9f560bca9cee86c55028bcea3d29e38d44c4e74fd58f9cd66441f720f22349d60524aa3aae37a3f6da0cea78ca6162ce3b6b6ae3626562d6db3822f35710a95af90f4ba4eac1372dbf941e1c81567410a05fa9caaf2")
key := hexKey("36a7edad64d51a568b00e51d3fa8cd340aa704153010edf7f55ab3066ca4ef21")
extra, _ := hex.DecodeString("01cf")
_, err := key.Decrypt(data, nil, extra)
if err != nil {
t.Fatal(err)
}
}