├── logo.png ├── xxhash ├── xxh3 │ ├── testdata │ │ └── Square Polano.txt │ ├── xxh3.go │ ├── xxh3_128.go │ └── xxh3_64.go ├── xxhash32 │ ├── sbox.go │ ├── xxhash32.go │ └── utils.go └── xxhash64 │ ├── sbox.go │ ├── xxhash64.go │ └── utils.go ├── go.mod ├── sm3 ├── block_generic.go ├── sm3.go ├── rand_test.go ├── block_arm64.go ├── block_amd64.go ├── sbox.go ├── binary_test.go ├── utils.go ├── rand_df.go ├── block.go └── const_asm.s ├── esch ├── sbox.go └── esch.go ├── haraka ├── init_amd64.go ├── haraka_stub.s ├── utils.go └── haraka.go ├── pmac ├── utils.go ├── verify.go ├── block.go └── aes │ └── pmac.go ├── k12 ├── keccakf_amd64.go ├── utils.go ├── k12.go └── xor.go ├── skeins ├── utils.go ├── sbox.go └── skeins.go ├── sha0 ├── sbox.go ├── sha0.go ├── utils.go └── sha0_test.go ├── go.sum ├── gmac ├── gcm │ └── gcm_noarch.go └── gmac_test.go ├── hash ├── md5.go ├── jh.go ├── md4.go ├── md2.go ├── sm3.go ├── adler32.go ├── has160.go ├── ripemd160.go ├── whirlpool.go ├── maphash.go ├── rabin.go ├── crc16.go ├── hash_helper.go ├── md5sha1.go ├── md2_test.go ├── hash_to.go ├── tiger.go ├── hash_test.go ├── hash_type_set.go ├── lsh256.go ├── blake256.go ├── blake512.go ├── hashing │ └── esch │ │ ├── esch256.go │ │ └── esch384.go ├── xxhash.go ├── keccak.go ├── tiger_test.go ├── crc64.go ├── md4_test.go ├── sha_test.go ├── echo.go ├── siphash_test.go ├── crc32.go ├── lsh512.go ├── blake512_test.go ├── sha3.go ├── siphash.go ├── hash_sum.go ├── hash_from.go ├── crc16_test.go ├── hash_use.go ├── hash.go ├── sm3_test.go ├── md5_test.go ├── whirlpool_test.go ├── gost.go ├── jh_test.go ├── hash_data_set.go └── blake.go ├── panama ├── panama.go └── utils.go ├── md2 ├── md2.go ├── binary_test.go ├── sbox.go └── binary.go ├── gost ├── gost34112012256 │ ├── hash.go │ ├── kdf.go │ ├── esptree.go │ └── hash_test.go ├── gost34112012512 │ ├── hash.go │ └── hash_test.go ├── gost34112012 │ └── hash_test.go └── gost341194 │ └── hash_test.go ├── jh ├── jh.go └── jh_test.go ├── has160 ├── has160.go ├── sbox.go └── utils.go ├── cmac ├── utils.go ├── verify.go └── aes │ └── cmac.go ├── hash_composition ├── hash_composition.go └── digest.go ├── ascon ├── utils.go └── mac_test.go ├── belt └── belt.go ├── chaskey └── chaskey.go ├── skein ├── skein256 │ ├── skein256_amd64.go │ └── skein256_ref.go ├── skein512_amd64.go ├── skein512_ref.go └── skein1024 │ ├── skein1024_ref.go │ └── skein1024_amd64.go ├── lyra2re2 ├── sum_test.go ├── lyra2re_test.go └── sum.go ├── kupyna ├── kmac.go ├── kupyna.go ├── utils.go ├── kupyna384.go ├── kmac256.go ├── kmac384.go └── kmac512.go ├── whirlpool ├── utils.go └── whirlpool.go ├── crc16 ├── x25 │ └── x25_test.go └── modbus │ └── modbus_test.go ├── radio_gatun ├── radio_gatun.go └── utils.go ├── siphash ├── utils.go └── siphash.go ├── rabin ├── poly_test.go └── rabin.go ├── md6 ├── sbox.go └── md6.go ├── echo └── utils.go ├── lyra2re ├── sum.go ├── lyra2re_test.go └── sum_test.go ├── jh2 ├── utils.go └── jh2.go ├── tiger └── tiger.go ├── luffa ├── utils.go └── luffa.go ├── fsb └── utils.go ├── shabal └── utils.go ├── shavite ├── utils.go └── shavite.go ├── bash └── sbox.go ├── streebog └── streebog.go ├── vmac ├── sbox.go └── utils.go ├── lsh256 └── lsh256.go ├── blake3 ├── sbox.go └── output.go ├── hamsi ├── utils.go └── hamsi.go ├── blake256 ├── utils.go ├── blake256.go └── binary_test.go ├── blake512 ├── blake512.go ├── binary_test.go └── utils.go ├── tool └── deepcopy │ └── deepcopy_test.go ├── murmur3 ├── digest.go └── murmur64.go ├── simd ├── utils.go └── simd.go ├── ripemd └── ripemd.go ├── skein512 ├── sbox.go └── skein.go ├── bmw ├── bmw.go └── utils.go ├── fugue └── fugue.go ├── makwa ├── params.go └── digest_test.go ├── groestl └── groestl.go └── lsh512 ├── lsh512_test.go └── lsh512.go /logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deatil/go-hash/HEAD/logo.png -------------------------------------------------------------------------------- /xxhash/xxh3/testdata/Square Polano.txt: -------------------------------------------------------------------------------- 1 | あのイーハトーヴォのすきとおった風、夏でも底に冷たさをもつ青いそら、うつくしい森で飾られたモリーオ市、郊外のぎらぎらひかる草の波。 2 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/deatil/go-hash 2 | 3 | go 1.20 4 | 5 | require golang.org/x/crypto v0.24.0 6 | 7 | require golang.org/x/sys v0.21.0 8 | -------------------------------------------------------------------------------- /sm3/block_generic.go: -------------------------------------------------------------------------------- 1 | //go:build purego || !(amd64 || arm64) 2 | 3 | package sm3 4 | 5 | func block(dig *digest, p []byte) { 6 | blockGeneric(dig, p) 7 | } 8 | -------------------------------------------------------------------------------- /xxhash/xxhash32/sbox.go: -------------------------------------------------------------------------------- 1 | package xxhash32 2 | 3 | var prime = []uint32{ 4 | 0x9e3779b1, 0x85ebca77, 5 | 0xc2b2ae3d, 0x27d4eb2f, 6 | 0x165667b1, 7 | } 8 | -------------------------------------------------------------------------------- /esch/sbox.go: -------------------------------------------------------------------------------- 1 | package esch 2 | 3 | var C = [8]uint32{ 4 | 0xb7e15162, 0xbf715880, 0x38b4da56, 0x324e7738, 5 | 0xbb1185eb, 0x4f7c7b57, 0xcfbfa1c8, 0xc2b3293d, 6 | } 7 | -------------------------------------------------------------------------------- /haraka/init_amd64.go: -------------------------------------------------------------------------------- 1 | // +build amd64 2 | 3 | package haraka 4 | 5 | import "golang.org/x/sys/cpu" 6 | 7 | func init() { 8 | hasAES = cpu.X86.HasAES 9 | } 10 | -------------------------------------------------------------------------------- /pmac/utils.go: -------------------------------------------------------------------------------- 1 | package pmac 2 | 3 | import ( 4 | "crypto/subtle" 5 | ) 6 | 7 | // XOR the contents of b into a in-place 8 | func xor(a, b []byte) { 9 | subtle.XORBytes(a, a, b) 10 | } 11 | -------------------------------------------------------------------------------- /xxhash/xxhash64/sbox.go: -------------------------------------------------------------------------------- 1 | package xxhash64 2 | 3 | var prime = []uint64{ 4 | 0x9e3779b185ebca87, 0xc2b2ae3d27d4eb4f, 5 | 0x165667b19e3779f9, 0x85ebca77c2b2ae63, 6 | 0x27d4eb2f165667c5, 7 | } 8 | -------------------------------------------------------------------------------- /k12/keccakf_amd64.go: -------------------------------------------------------------------------------- 1 | //go:build amd64 && !purego && gc 2 | 3 | package k12 4 | 5 | // This function is implemented in keccakf_amd64.s. 6 | 7 | //go:noescape 8 | 9 | func keccakF1600(a *[25]uint64, nr int) 10 | -------------------------------------------------------------------------------- /skeins/utils.go: -------------------------------------------------------------------------------- 1 | package skeins 2 | 3 | import ( 4 | "encoding/binary" 5 | ) 6 | 7 | func getu64(ptr []byte) uint64 { 8 | return binary.LittleEndian.Uint64(ptr) 9 | } 10 | 11 | func putu64(ptr []byte, a uint64) { 12 | binary.LittleEndian.PutUint64(ptr, a) 13 | } 14 | -------------------------------------------------------------------------------- /haraka/haraka_stub.s: -------------------------------------------------------------------------------- 1 | // +build !amd64 2 | 3 | #include "textflag.h" 4 | 5 | // func haraka256AES(rc *uint32, dst, src *byte) 6 | TEXT ·haraka256AES(SB),NOSPLIT,$0 7 | RET 8 | // func haraka512AES(rc *uint32, dst, src *byte) 9 | TEXT ·haraka512AES(SB),NOSPLIT,$0 10 | RET 11 | -------------------------------------------------------------------------------- /sha0/sbox.go: -------------------------------------------------------------------------------- 1 | package sha0 2 | 3 | var initVal = [5]uint32{ 4 | 0x67452301, 5 | 0xEFCDAB89, 6 | 0x98BADCFE, 7 | 0x10325476, 8 | 0xC3D2E1F0, 9 | } 10 | 11 | var sbox = []uint32{ 12 | 0x5A827999, 13 | 0x6ED9EBA1, 14 | 0x8F1BBCDC, 15 | 0xCA62C1D6, 16 | } 17 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI= 2 | golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM= 3 | golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= 4 | golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 5 | -------------------------------------------------------------------------------- /gmac/gcm/gcm_noarch.go: -------------------------------------------------------------------------------- 1 | //go:build !((amd64 || arm64) && !purego && (!gccgo || go1.18)) 2 | // +build !amd64,!arm64 purego gccgo,!go1.18 3 | 4 | package gcm 5 | 6 | func init() { 7 | gcmInit = gcmInitGo 8 | gcmDeriveCounter = gcmDeriveCounterGo 9 | gcmUpdate = gcmUpdateGo 10 | gcmAuth = gcmAuthGo 11 | gcmFinish = gcmFinishGo 12 | } 13 | -------------------------------------------------------------------------------- /hash/md5.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "crypto/md5" 5 | ) 6 | 7 | // MD5 8 | func (this Hash) MD5() Hash { 9 | h := md5.New() 10 | h.Write(this.data) 11 | 12 | this.data = h.Sum(nil) 13 | 14 | return this 15 | } 16 | 17 | // NewMD5 18 | func (this Hash) NewMD5() Hash { 19 | this.hash = md5.New() 20 | 21 | return this 22 | } 23 | -------------------------------------------------------------------------------- /hash/jh.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "github.com/deatil/go-hash/jh" 5 | ) 6 | 7 | // 国密 jh 签名 8 | func (this Hash) JH() Hash { 9 | h := jh.New() 10 | h.Write(this.data) 11 | 12 | this.data = h.Sum(nil) 13 | 14 | return this 15 | } 16 | 17 | // NewJH 18 | func (this Hash) NewJH() Hash { 19 | this.hash = jh.New() 20 | 21 | return this 22 | } 23 | -------------------------------------------------------------------------------- /hash/md4.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "golang.org/x/crypto/md4" 5 | ) 6 | 7 | // MD4 哈希值 8 | func (this Hash) MD4() Hash { 9 | h := md4.New() 10 | h.Write(this.data) 11 | 12 | this.data = h.Sum(nil) 13 | 14 | return this 15 | } 16 | 17 | // NewMD4 18 | func (this Hash) NewMD4() Hash { 19 | this.hash = md4.New() 20 | 21 | return this 22 | } 23 | -------------------------------------------------------------------------------- /hash/md2.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "github.com/deatil/go-hash/md2" 5 | ) 6 | 7 | // md2 签名 8 | func (this Hash) MD2() Hash { 9 | h := md2.New() 10 | h.Write(this.data) 11 | 12 | this.data = h.Sum(nil) 13 | 14 | return this 15 | } 16 | 17 | // NewMD2 18 | func (this Hash) NewMD2() Hash { 19 | this.hash = md2.New() 20 | 21 | return this 22 | } 23 | -------------------------------------------------------------------------------- /hash/sm3.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "github.com/deatil/go-hash/sm3" 5 | ) 6 | 7 | // 国密 sm3 签名 8 | func (this Hash) SM3() Hash { 9 | h := sm3.New() 10 | h.Write(this.data) 11 | 12 | this.data = h.Sum(nil) 13 | 14 | return this 15 | } 16 | 17 | // NewSM3 18 | func (this Hash) NewSM3() Hash { 19 | this.hash = sm3.New() 20 | 21 | return this 22 | } 23 | -------------------------------------------------------------------------------- /hash/adler32.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "hash/adler32" 5 | ) 6 | 7 | // Adler32 8 | func (this Hash) Adler32() Hash { 9 | h := adler32.New() 10 | h.Write(this.data) 11 | 12 | this.data = h.Sum(nil) 13 | 14 | return this 15 | } 16 | 17 | // NewAdler32 18 | func (this Hash) NewAdler32() Hash { 19 | this.hash = adler32.New() 20 | 21 | return this 22 | } 23 | -------------------------------------------------------------------------------- /panama/panama.go: -------------------------------------------------------------------------------- 1 | package panama 2 | 3 | import ( 4 | "hash" 5 | ) 6 | 7 | // New returns a new hash.Hash. 8 | func New() hash.Hash { 9 | return newDigest() 10 | } 11 | 12 | // Sum returns the panama checksum of the data. 13 | func Sum(data []byte) (out [Size]byte) { 14 | h := New() 15 | h.Write(data) 16 | sum := h.Sum(nil) 17 | 18 | copy(out[:], sum) 19 | return 20 | } 21 | -------------------------------------------------------------------------------- /hash/has160.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "github.com/deatil/go-hash/has160" 5 | ) 6 | 7 | // HAS160 8 | func (this Hash) HAS160() Hash { 9 | h := has160.New() 10 | h.Write(this.data) 11 | 12 | this.data = h.Sum(nil) 13 | 14 | return this 15 | } 16 | 17 | // NewHAS160 18 | func (this Hash) NewHAS160() Hash { 19 | this.hash = has160.New() 20 | 21 | return this 22 | } 23 | -------------------------------------------------------------------------------- /md2/md2.go: -------------------------------------------------------------------------------- 1 | package md2 2 | 3 | import ( 4 | "hash" 5 | ) 6 | 7 | // New returns a new hash.Hash computing the MD2 checksum. 8 | func New() hash.Hash { 9 | return newDigest() 10 | } 11 | 12 | // Sum returns checksum of the data. 13 | func Sum(data []byte) (sum [Size]byte) { 14 | h := New() 15 | h.Write(data) 16 | hash := h.Sum(nil) 17 | 18 | copy(sum[:], hash) 19 | return 20 | } 21 | -------------------------------------------------------------------------------- /xxhash/xxh3/xxh3.go: -------------------------------------------------------------------------------- 1 | package xxh3 2 | 3 | import ( 4 | "hash" 5 | ) 6 | 7 | // XXH128_hash_t 8 | type Uint128 struct { 9 | Low, High uint64 10 | } 11 | 12 | func (u Uint128) Bytes() (out [16]byte) { 13 | putu64be(out[0:], u.High) 14 | putu64be(out[8:], u.Low) 15 | 16 | return 17 | } 18 | 19 | // Hash128 20 | type Hash128 interface { 21 | hash.Hash 22 | Sum128() Uint128 23 | } 24 | -------------------------------------------------------------------------------- /gost/gost34112012256/hash.go: -------------------------------------------------------------------------------- 1 | package gost34112012256 2 | 3 | import ( 4 | "hash" 5 | 6 | "github.com/deatil/go-hash/gost/gost34112012" 7 | ) 8 | 9 | // GOST R 34.11-2012 256-bit hash function. 10 | // RFC 6986. Big-endian hash output. 11 | 12 | const ( 13 | Size = 32 14 | BlockSize = gost34112012.BlockSize 15 | ) 16 | 17 | func New() hash.Hash { 18 | return gost34112012.New(32) 19 | } 20 | -------------------------------------------------------------------------------- /gost/gost34112012512/hash.go: -------------------------------------------------------------------------------- 1 | package gost34112012512 2 | 3 | import ( 4 | "hash" 5 | 6 | "github.com/deatil/go-hash/gost/gost34112012" 7 | ) 8 | 9 | // GOST R 34.11-2012 512-bit hash function. 10 | // RFC 6986. Big-endian hash output. 11 | 12 | const ( 13 | Size = 64 14 | BlockSize = gost34112012.BlockSize 15 | ) 16 | 17 | func New() hash.Hash { 18 | return gost34112012.New(64) 19 | } 20 | -------------------------------------------------------------------------------- /sha0/sha0.go: -------------------------------------------------------------------------------- 1 | package sha0 2 | 3 | import ( 4 | "hash" 5 | ) 6 | 7 | // New returns a new hash.Hash computing the sha0 checksum 8 | func New() hash.Hash { 9 | return newDigest() 10 | } 11 | 12 | // Sum returns the sha0 checksum of the data. 13 | func Sum(data []byte) (out [Size]byte) { 14 | h := New() 15 | h.Write(data) 16 | sum := h.Sum(nil) 17 | 18 | copy(out[:], sum) 19 | return 20 | } 21 | -------------------------------------------------------------------------------- /sm3/sm3.go: -------------------------------------------------------------------------------- 1 | package sm3 2 | 3 | import ( 4 | "hash" 5 | ) 6 | 7 | // New returns a new hash.Hash computing the SM3 checksum. 8 | func New() hash.Hash { 9 | return newDigest() 10 | } 11 | 12 | // Sum returns the SM3 checksum of the data. 13 | func Sum(data []byte) (sum [Size]byte) { 14 | h := New() 15 | h.Write(data) 16 | hash := h.Sum(nil) 17 | 18 | copy(sum[:], hash) 19 | return 20 | } 21 | -------------------------------------------------------------------------------- /hash/ripemd160.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "golang.org/x/crypto/ripemd160" 5 | ) 6 | 7 | // Ripemd160 哈希值 8 | func (this Hash) Ripemd160() Hash { 9 | h := ripemd160.New() 10 | h.Write(this.data) 11 | 12 | this.data = h.Sum(nil) 13 | 14 | return this 15 | } 16 | 17 | // NewRipemd160 18 | func (this Hash) NewRipemd160() Hash { 19 | this.hash = ripemd160.New() 20 | 21 | return this 22 | } 23 | -------------------------------------------------------------------------------- /hash/whirlpool.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "github.com/deatil/go-hash/whirlpool" 5 | ) 6 | 7 | // Whirlpool 8 | func (this Hash) Whirlpool() Hash { 9 | h := whirlpool.New() 10 | h.Write(this.data) 11 | 12 | this.data = h.Sum(nil) 13 | 14 | return this 15 | } 16 | 17 | // NewWhirlpool 18 | func (this Hash) NewWhirlpool() Hash { 19 | this.hash = whirlpool.New() 20 | 21 | return this 22 | } 23 | -------------------------------------------------------------------------------- /jh/jh.go: -------------------------------------------------------------------------------- 1 | // Package jh implements JH-256 algorithm. 2 | package jh 3 | 4 | import ( 5 | "hash" 6 | ) 7 | 8 | // New returns a new hash.Hash computing the JH-256 checksum 9 | func New() hash.Hash { 10 | return newDigest() 11 | } 12 | 13 | func Sum(data []byte) (sum [Size]byte) { 14 | var h digest 15 | h.Reset() 16 | h.Write(data) 17 | 18 | hash := h.Sum(nil) 19 | copy(sum[:], hash) 20 | return 21 | } 22 | -------------------------------------------------------------------------------- /has160/has160.go: -------------------------------------------------------------------------------- 1 | package has160 2 | 3 | import ( 4 | "hash" 5 | ) 6 | 7 | // New returns a new hash.Hash computing the HAS-160 checksum. 8 | func New() hash.Hash { 9 | return newDigest() 10 | } 11 | 12 | // Sum returns the HAS-160 checksum of the data. 13 | func Sum(data []byte) (sum [Size]byte) { 14 | var h digest 15 | h.Reset() 16 | h.Write(data) 17 | 18 | hash := h.Sum(nil) 19 | copy(sum[:], hash) 20 | return 21 | } 22 | -------------------------------------------------------------------------------- /cmac/utils.go: -------------------------------------------------------------------------------- 1 | package cmac 2 | 3 | import ( 4 | "crypto/subtle" 5 | ) 6 | 7 | func shift(dst, src []byte) int { 8 | var b, bit byte 9 | for i := len(src) - 1; i >= 0; i-- { // a range would be nice 10 | bit = src[i] >> 7 11 | dst[i] = src[i]<<1 | b 12 | b = bit 13 | } 14 | 15 | return int(b) 16 | } 17 | 18 | // XOR the contents of b into a in-place 19 | func xor(a, b []byte) { 20 | subtle.XORBytes(a, a, b) 21 | } 22 | -------------------------------------------------------------------------------- /hash/maphash.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "hash" 5 | "hash/maphash" 6 | ) 7 | 8 | // newMaphash 9 | func newMaphash() hash.Hash64 { 10 | return &maphash.Hash{} 11 | } 12 | 13 | // Maphash 14 | func (this Hash) Maphash() Hash { 15 | h := newMaphash() 16 | h.Write(this.data) 17 | 18 | this.data = h.Sum(nil) 19 | 20 | return this 21 | } 22 | 23 | // NewMaphash 24 | func (this Hash) NewMaphash() Hash { 25 | this.hash = newMaphash() 26 | 27 | return this 28 | } 29 | -------------------------------------------------------------------------------- /hash_composition/hash_composition.go: -------------------------------------------------------------------------------- 1 | package hash_composition 2 | 3 | import ( 4 | "hash" 5 | ) 6 | 7 | // New returns a new hash.Hash computing the hash_composition checksum 8 | func New(h1, h2 func() hash.Hash) hash.Hash { 9 | return newDigest(h1, h2) 10 | } 11 | 12 | // Sum returns the hash_composition checksum of the data. 13 | func Sum(h1, h2 func() hash.Hash, data []byte) (sum []byte) { 14 | h := New(h1, h2) 15 | h.Write(data) 16 | sum = h.Sum(nil) 17 | 18 | return 19 | } 20 | -------------------------------------------------------------------------------- /sm3/rand_test.go: -------------------------------------------------------------------------------- 1 | package sm3 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | func Test_Rand(t *testing.T) { 8 | nonce := []byte("nonce-asdfg") 9 | label := []byte("label-asdfg") 10 | addin := []byte("addin-ftgyj") 11 | 12 | out := make([]byte, 16) 13 | 14 | r := NewRand(nonce, label) 15 | r.Generate(out, addin) 16 | 17 | if len(out) == 0 { 18 | t.Error("Rand make error") 19 | } 20 | 21 | if len(out) != 16 { 22 | t.Error("Rand make length error") 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /hash/rabin.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "github.com/deatil/go-hash/rabin" 5 | ) 6 | 7 | // 国密 rabin 签名 8 | func (this Hash) Rabin(polynomial uint64, window int) Hash { 9 | h := rabin.New(rabin.NewTable(polynomial, window)) 10 | h.Write(this.data) 11 | 12 | this.data = h.Sum(nil) 13 | 14 | return this 15 | } 16 | 17 | // NewRabin 18 | func (this Hash) NewRabin(polynomial uint64, window int) Hash { 19 | this.hash = rabin.New(rabin.NewTable(polynomial, window)) 20 | 21 | return this 22 | } 23 | -------------------------------------------------------------------------------- /ascon/utils.go: -------------------------------------------------------------------------------- 1 | package ascon 2 | 3 | import ( 4 | "math/bits" 5 | "encoding/binary" 6 | ) 7 | 8 | func getu64(b []byte) uint64 { 9 | return binary.BigEndian.Uint64(b) 10 | } 11 | 12 | func putu64(b []byte, x uint64) { 13 | binary.BigEndian.PutUint64(b, x) 14 | } 15 | 16 | func appendu64(b []byte, x uint64) []byte { 17 | return append(b, byte(x>>56), byte(x>>48), byte(x>>40), byte(x>>32), byte(x>>24), byte(x>>16), byte(x>>8), byte(x)) 18 | } 19 | 20 | func rotl(a uint64, n int) uint64 { 21 | return bits.RotateLeft64(a, n) 22 | } 23 | -------------------------------------------------------------------------------- /belt/belt.go: -------------------------------------------------------------------------------- 1 | package belt 2 | 3 | import ( 4 | "hash" 5 | ) 6 | 7 | /* 8 | * This is an implementation of the BELT-HASH hash function as 9 | * defined int STB 34.101.31. 10 | */ 11 | 12 | // New returns a new hash.Hash computing the BELT checksum 13 | func New() hash.Hash { 14 | return newDigest() 15 | } 16 | 17 | // Sum returns the BELT checksum of the data. 18 | func Sum(data []byte) (sum [Size]byte) { 19 | h := New() 20 | h.Write(data) 21 | hashed := h.Sum(nil) 22 | 23 | copy(sum[:], hashed[:Size]) 24 | return 25 | } 26 | -------------------------------------------------------------------------------- /gost/gost34112012512/hash_test.go: -------------------------------------------------------------------------------- 1 | package gost34112012512 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func Test_Check(t *testing.T) { 9 | in := []byte("gost34112012512-asdfg") 10 | check := "f6e5e348001a4ee3a1299c5283ddae617655353fcfc3d79c81f9c01470bbef58075b0514c0b03187a3c1bb7a24383664abac0fbc2019555ec65b9a7d972bf864" 11 | 12 | h := New() 13 | h.Write(in) 14 | 15 | out := h.Sum(nil) 16 | 17 | if fmt.Sprintf("%x", out) != check { 18 | t.Errorf("Check error. got %x, want %s", out, check) 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /hash/crc16.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "github.com/deatil/go-hash/crc16/x25" 5 | "github.com/deatil/go-hash/crc16/modbus" 6 | ) 7 | 8 | // CRC16_X25 哈希值 9 | func (this Hash) CRC16_X25() Hash { 10 | data := x25.Checksum(string(this.data)) 11 | 12 | this.data, this.Error = this.HexDecode(data) 13 | 14 | return this 15 | } 16 | 17 | // CRC16_Modbus 哈希值 18 | func (this Hash) CRC16_Modbus() Hash { 19 | data := modbus.Checksum(string(this.data)) 20 | 21 | this.data, this.Error = this.HexDecode(data) 22 | 23 | return this 24 | } 25 | -------------------------------------------------------------------------------- /chaskey/chaskey.go: -------------------------------------------------------------------------------- 1 | package chaskey 2 | 3 | import ( 4 | "hash" 5 | ) 6 | 7 | /* 8 | 9 | http://mouha.be/chaskey/ 10 | 11 | https://eprint.iacr.org/2014/386.pdf 12 | https://eprint.iacr.org/2015/1182.pdf 13 | 14 | */ 15 | 16 | // New returns a new hash.Hash computing the chaskey checksum 17 | // New returns a new 8-round chaskey hasher. 18 | func New(key []byte) (hash.Hash, error) { 19 | return newDigest(key, 8) 20 | } 21 | 22 | // New12 returns a new 12-round chaskey hasher. 23 | func New12(key []byte) (hash.Hash, error) { 24 | return newDigest(key, 12) 25 | } 26 | -------------------------------------------------------------------------------- /skein/skein256/skein256_amd64.go: -------------------------------------------------------------------------------- 1 | // +build amd64 2 | 3 | package skein256 4 | 5 | import "unsafe" 6 | 7 | func bytesToBlock(block *[4]uint64, src []byte) { 8 | srcPtr := (*[4]uint64)(unsafe.Pointer(&src[0])) 9 | 10 | block[0] = srcPtr[0] 11 | block[1] = srcPtr[1] 12 | block[2] = srcPtr[2] 13 | block[3] = srcPtr[3] 14 | } 15 | 16 | func blockToBytes(dst []byte, block *[4]uint64) { 17 | dstPtr := (*[4]uint64)(unsafe.Pointer(&dst[0])) 18 | 19 | dstPtr[0] = block[0] 20 | dstPtr[1] = block[1] 21 | dstPtr[2] = block[2] 22 | dstPtr[3] = block[3] 23 | } 24 | -------------------------------------------------------------------------------- /k12/utils.go: -------------------------------------------------------------------------------- 1 | package k12 2 | 3 | import ( 4 | "encoding/binary" 5 | ) 6 | 7 | // Encodes the length of a value. It is used in the final padding of K12 8 | func rightEncode(value uint64) []byte { 9 | var input [9]byte 10 | var offset int 11 | 12 | if value == 0 { 13 | offset = 8 14 | } else { 15 | binary.BigEndian.PutUint64(input[0:], value) 16 | for offset = 0; offset < 9; offset++ { 17 | if input[offset] != 0 { 18 | break 19 | } 20 | } 21 | } 22 | 23 | input[8] = byte(8 - offset) 24 | return input[offset:] 25 | } 26 | -------------------------------------------------------------------------------- /lyra2re2/sum_test.go: -------------------------------------------------------------------------------- 1 | package lyra2re2 2 | 3 | import ( 4 | "bytes" 5 | "testing" 6 | "encoding/hex" 7 | ) 8 | 9 | func Test_SumV2(t *testing.T) { 10 | correct, err := hex.DecodeString("5f21d7763b1ae8fc87db7dc993ddc50468765729411ba6b24906de15851a4abf") 11 | if err != nil { 12 | t.Fatal(err) 13 | } 14 | 15 | data := make([]byte, 80) 16 | copy(data, []byte("test")) 17 | result, err := Sum(data) 18 | if err != nil { 19 | t.Fatal(err) 20 | } 21 | if !bytes.Equal(correct, result) { 22 | t.Errorf("not match, got %x, want %x", result, correct) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /sm3/block_arm64.go: -------------------------------------------------------------------------------- 1 | //go:build !purego 2 | 3 | package sm3 4 | 5 | import ( 6 | "os" 7 | "golang.org/x/sys/cpu" 8 | ) 9 | 10 | // 汇编部分来自于 github.com/emmansun/gmsm/sm3 11 | 12 | var useSM3NI = cpu.ARM64.HasSM3 && os.Getenv("DISABLE_SM3NI") != "1" 13 | 14 | var t = []uint32{ 15 | 0x79cc4519, 16 | 0x9d8a7a87, 17 | } 18 | 19 | //go:noescape 20 | func blockARM64(dig *digest, p []byte) 21 | 22 | //go:noescape 23 | func blockSM3NI(h []uint32, p []byte, t []uint32) 24 | 25 | func block(dig *digest, p []byte) { 26 | if !useSM3NI { 27 | blockARM64(dig, p) 28 | } else { 29 | h := dig.s[:] 30 | blockSM3NI(h, p, t) 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /hash/hash_helper.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "strings" 5 | ) 6 | 7 | // MD5_16 8 | func MD5_16(s string) string { 9 | data := FromString(s).MD5().ToHexString() 10 | 11 | return data[8:24] 12 | } 13 | 14 | // CRC 左侧补码 0 15 | func LeftCRCPadding(text string, size int) string { 16 | n := len(text) 17 | if n == 0 || size < 1 { 18 | return text 19 | } 20 | 21 | paddingSize := size - n%size 22 | 23 | // 为 0 时不补位 24 | if paddingSize == size { 25 | return text 26 | } 27 | 28 | paddingText := strings.Repeat("0", paddingSize) 29 | 30 | return paddingText + text 31 | } 32 | -------------------------------------------------------------------------------- /jh/jh_test.go: -------------------------------------------------------------------------------- 1 | package jh 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func Test_Check(t *testing.T) { 9 | in := []byte("nonce-asdfg") 10 | check := "84bcd622325787f4096e4aae5fbec20f1dd041ba22785a2e49d9b9ad0f976afa" 11 | 12 | h := New() 13 | h.Write(in) 14 | 15 | out := h.Sum(nil) 16 | 17 | if fmt.Sprintf("%x", out) != check { 18 | t.Errorf("Check error. got %x, want %s", out, check) 19 | } 20 | 21 | // ========== 22 | 23 | out2 := Sum(in) 24 | 25 | if fmt.Sprintf("%x", out2) != check { 26 | t.Errorf("Check error. got %x, want %s", out2, check) 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /kupyna/kmac.go: -------------------------------------------------------------------------------- 1 | package kupyna 2 | 3 | import ( 4 | "fmt" 5 | "hash" 6 | ) 7 | 8 | type KeySizeError int 9 | 10 | func (k KeySizeError) Error() string { 11 | return fmt.Sprintf("go-hash/kupyna: invalid key size %d", int(k)) 12 | } 13 | 14 | // NewKmac returns a new hash.Hash computing the Kmac checksum 15 | func NewKmac(key []byte) (hash.Hash, error) { 16 | l := len(key) 17 | switch l { 18 | case 32: 19 | return NewKmac256(key) 20 | case 48: 21 | return NewKmac384(key) 22 | case 64: 23 | return NewKmac512(key) 24 | } 25 | 26 | return nil, KeySizeError(l) 27 | } 28 | -------------------------------------------------------------------------------- /whirlpool/utils.go: -------------------------------------------------------------------------------- 1 | package whirlpool 2 | 3 | import ( 4 | "encoding/binary" 5 | ) 6 | 7 | func bytesToUint64s(b []byte) []uint64 { 8 | size := len(b) / 8 9 | dst := make([]uint64, size) 10 | 11 | for i := 0; i < size; i++ { 12 | j := i * 8 13 | 14 | dst[i] = binary.BigEndian.Uint64(b[j:]) 15 | } 16 | 17 | return dst 18 | } 19 | 20 | func uint64sToBytes(w []uint64) []byte { 21 | size := len(w) * 8 22 | dst := make([]byte, size) 23 | 24 | for i := 0; i < len(w); i++ { 25 | j := i * 8 26 | 27 | binary.BigEndian.PutUint64(dst[j:], w[i]) 28 | } 29 | 30 | return dst 31 | } 32 | 33 | -------------------------------------------------------------------------------- /hash/md5sha1.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "crypto/md5" 5 | "crypto/sha1" 6 | ) 7 | 8 | func sha1Hash(slice []byte) []byte { 9 | h := sha1.New() 10 | h.Write(slice) 11 | 12 | return h.Sum(nil) 13 | } 14 | 15 | // MD5SHA1 哈希值 16 | func md5Sha1(slice []byte) []byte { 17 | md5sha1 := make([]byte, md5.Size + sha1.Size) 18 | 19 | h := md5.New() 20 | h.Write(slice) 21 | 22 | copy(md5sha1, h.Sum(nil)) 23 | copy(md5sha1[md5.Size:], sha1Hash(slice)) 24 | 25 | return md5sha1[:] 26 | } 27 | 28 | // MD5SHA1 哈希值 29 | func (this Hash) MD5SHA1() Hash { 30 | this.data = md5Sha1(this.data) 31 | 32 | return this 33 | } 34 | 35 | -------------------------------------------------------------------------------- /has160/sbox.go: -------------------------------------------------------------------------------- 1 | package has160 2 | 3 | var ( 4 | initH = [...]uint32{ 5 | 0x67452301, 6 | 0xefcdab89, 7 | 0x98badcfe, 8 | 0x10325476, 9 | 0xc3d2e1f0, 10 | } 11 | 12 | s1 = [...]int{5, 11, 7, 15, 6, 13, 8, 14, 7, 12, 9, 11, 8, 15, 6, 12, 9, 14, 5, 13} 13 | 14 | x1 = [...]int{18, 0, 1, 2, 3, 19, 4, 5, 6, 7, 16, 8, 9, 10, 11, 17, 12, 13, 14, 15} 15 | x2 = [...]int{18, 3, 6, 9, 12, 19, 15, 2, 5, 8, 16, 11, 14, 1, 4, 17, 7, 10, 13, 0} 16 | x3 = [...]int{18, 12, 5, 14, 7, 19, 0, 9, 2, 11, 16, 4, 13, 6, 15, 17, 8, 1, 10, 3} 17 | x4 = [...]int{18, 7, 2, 13, 8, 19, 3, 14, 9, 4, 16, 15, 10, 5, 0, 17, 11, 6, 1, 12} 18 | ) 19 | -------------------------------------------------------------------------------- /sm3/block_amd64.go: -------------------------------------------------------------------------------- 1 | //go:build !purego 2 | 3 | package sm3 4 | 5 | import "golang.org/x/sys/cpu" 6 | 7 | // 汇编部分来自于 github.com/emmansun/gmsm/sm3 8 | 9 | var useAVX2 = cpu.X86.HasAVX2 && cpu.X86.HasBMI2 10 | var useAVX = cpu.X86.HasAVX 11 | var useSSSE3 = cpu.X86.HasSSSE3 12 | 13 | //go:noescape 14 | func blockAMD64(dig *digest, p []byte) 15 | 16 | //go:noescape 17 | func blockSIMD(dig *digest, p []byte) 18 | 19 | //go:noescape 20 | func blockAVX2(dig *digest, p []byte) 21 | 22 | func block(dig *digest, p []byte) { 23 | if useAVX2 { 24 | blockAVX2(dig, p) 25 | } else if useSSSE3 || useAVX { 26 | blockSIMD(dig, p) 27 | } else { 28 | blockAMD64(dig, p) 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /hash/md2_test.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | func Test_MD2(t *testing.T) { 8 | eq := assertT(t) 9 | err := assertErrorT(t) 10 | 11 | msg := "abcdefghijklmnopqrstuvwxyz" 12 | md := "4e8ddff3650292ab5a4108c3aa47940b" 13 | 14 | t.Run("Sum", func(t *testing.T) { 15 | e := FromString(msg).MD2() 16 | 17 | err(e.Error, "Sum") 18 | eq(e.ToHexString(), md, "Sum") 19 | }) 20 | 21 | t.Run("New", func(t *testing.T) { 22 | e := Hashing(). 23 | NewMD2(). 24 | Write([]byte(msg)). 25 | Sum(nil) 26 | 27 | err(e.Error, "New") 28 | eq(e.ToHexString(), md, "New") 29 | }) 30 | } 31 | -------------------------------------------------------------------------------- /hash/hash_to.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "io" 5 | "bytes" 6 | ) 7 | 8 | // 输出字符 9 | func (this Hash) String() string { 10 | return this.ToString() 11 | } 12 | 13 | // 输出字节 14 | func (this Hash) ToBytes() []byte { 15 | return this.data 16 | } 17 | 18 | // 输出字符 19 | func (this Hash) ToString() string { 20 | return string(this.data) 21 | } 22 | 23 | // 输出Base64 24 | func (this Hash) ToBase64String() string { 25 | return this.Base64Encode(this.data) 26 | } 27 | 28 | // 输出Hex 29 | func (this Hash) ToHexString() string { 30 | return this.HexEncode(this.data) 31 | } 32 | 33 | // 输出 io.Reader 34 | func (this Hash) ToReader() io.Reader { 35 | return bytes.NewBuffer(this.data) 36 | } 37 | -------------------------------------------------------------------------------- /crc16/x25/x25_test.go: -------------------------------------------------------------------------------- 1 | package x25 2 | 3 | import ( 4 | "fmt" 5 | "strings" 6 | "testing" 7 | ) 8 | 9 | var tests = []struct { 10 | input string 11 | output string 12 | }{ 13 | {"sdfgsdgfsdfg123132", "FA73"}, 14 | {"dfg.;kp[jewijr0-34ls", "4938"}, 15 | {"123123", "C56A"}, 16 | } 17 | 18 | func Test_CRC16(t *testing.T) { 19 | for index, test := range tests { 20 | crcData := Checksum(test.input) 21 | crcData = strings.ToUpper(crcData) 22 | 23 | t.Run(fmt.Sprintf("CRC16 %d", index), func(t *testing.T) { 24 | if test.output != crcData { 25 | t.Errorf("[%d] got %s, want %s", index, crcData, test.output) 26 | } 27 | }) 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /hash/tiger.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "github.com/deatil/go-hash/tiger" 5 | ) 6 | 7 | // Tiger 8 | func (this Hash) Tiger() Hash { 9 | h := tiger.New() 10 | h.Write(this.data) 11 | 12 | this.data = h.Sum(nil) 13 | 14 | return this 15 | } 16 | 17 | // NewTiger 18 | func (this Hash) NewTiger() Hash { 19 | this.hash = tiger.New() 20 | 21 | return this 22 | } 23 | 24 | // =============== 25 | 26 | 27 | // Tiger 28 | func (this Hash) Tiger2() Hash { 29 | h := tiger.New2() 30 | h.Write(this.data) 31 | 32 | this.data = h.Sum(nil) 33 | 34 | return this 35 | } 36 | 37 | // NewTiger 38 | func (this Hash) NewTiger2() Hash { 39 | this.hash = tiger.New2() 40 | 41 | return this 42 | } 43 | -------------------------------------------------------------------------------- /crc16/modbus/modbus_test.go: -------------------------------------------------------------------------------- 1 | package modbus 2 | 3 | import ( 4 | "fmt" 5 | "strings" 6 | "testing" 7 | ) 8 | 9 | var tests = []struct { 10 | input string 11 | output string 12 | }{ 13 | {"sdfgsdgfsdfg123132", "381B"}, 14 | {"dfg.;kp[jewijr0-34ls", "2D12"}, 15 | {"123123", "36"}, 16 | } 17 | 18 | func Test_CRC16(t *testing.T) { 19 | for index, test := range tests { 20 | crcData := Checksum(test.input) 21 | crcData = strings.ToUpper(crcData) 22 | 23 | t.Run(fmt.Sprintf("CRC16 %d", index), func(t *testing.T) { 24 | if test.output != crcData { 25 | t.Errorf("[%d] got %s, want %s", index, crcData, test.output) 26 | } 27 | }) 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /hash/hash_test.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "testing" 5 | "reflect" 6 | "encoding/hex" 7 | ) 8 | 9 | func fromHex(s string) []byte { 10 | h, _ := hex.DecodeString(s) 11 | return h 12 | } 13 | 14 | func assertT(t *testing.T) func(any, any, string) { 15 | return func(actual any, expected any, msg string) { 16 | if !reflect.DeepEqual(actual, expected) { 17 | t.Errorf("Failed %s: actual: %v, expected: %v", msg, actual, expected) 18 | } 19 | } 20 | } 21 | 22 | func assertErrorT(t *testing.T) func(error, string) { 23 | return func(err error, msg string) { 24 | if err != nil { 25 | t.Errorf("Failed %s: error: %+v", msg, err) 26 | } 27 | } 28 | } 29 | 30 | -------------------------------------------------------------------------------- /hash/hash_type_set.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | // Type Name 4 | type TypeName interface { 5 | ~uint | ~int 6 | } 7 | 8 | // Type Set 9 | type TypeSet[N TypeName, D any] struct { 10 | // 最大值 11 | max N 12 | 13 | // 数据 14 | names *DataSet[N, D] 15 | } 16 | 17 | // New TypeSet 18 | func NewTypeSet[N TypeName, D any](max N) *TypeSet[N, D] { 19 | return &TypeSet[N, D]{ 20 | max: max, 21 | names: NewDataSet[N, D](), 22 | } 23 | } 24 | 25 | // 生成新序列 26 | // Generate new id 27 | func (this *TypeSet[N, D]) Generate() N { 28 | old := this.max 29 | this.max++ 30 | 31 | return old 32 | } 33 | 34 | // 类型名称列表 35 | // name list 36 | func (this *TypeSet[N, D]) Names() *DataSet[N, D] { 37 | return this.names 38 | } 39 | -------------------------------------------------------------------------------- /kupyna/kupyna.go: -------------------------------------------------------------------------------- 1 | package kupyna 2 | 3 | // Sum256 returns the Kupyna-256 checksum of the data. 4 | func Sum256(data []byte) (sum [Size256]byte) { 5 | h := New256() 6 | h.Write(data) 7 | 8 | hash := h.Sum(nil) 9 | copy(sum[:], hash) 10 | return 11 | } 12 | 13 | // Sum384 returns the Kupyna-384 checksum of the data. 14 | func Sum384(data []byte) (sum [Size384]byte) { 15 | h := New384() 16 | h.Write(data) 17 | 18 | hash := h.Sum(nil) 19 | copy(sum[:], hash) 20 | return 21 | } 22 | 23 | // Sum512 returns the Kupyna-512 checksum of the data. 24 | func Sum512(data []byte) (sum [Size512]byte) { 25 | h := New512() 26 | h.Write(data) 27 | 28 | hash := h.Sum(nil) 29 | copy(sum[:], hash) 30 | return 31 | } 32 | -------------------------------------------------------------------------------- /hash/lsh256.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "github.com/deatil/go-hash/lsh256" 5 | ) 6 | 7 | // LSH256 8 | func (this Hash) LSH256() Hash { 9 | h := lsh256.New() 10 | h.Write(this.data) 11 | 12 | this.data = h.Sum(nil) 13 | 14 | return this 15 | } 16 | 17 | // NewHAS160 18 | func (this Hash) NewLSH256() Hash { 19 | this.hash = lsh256.New() 20 | 21 | return this 22 | } 23 | 24 | // =========== 25 | 26 | // LSH256_224 27 | func (this Hash) LSH256_224() Hash { 28 | h := lsh256.New224() 29 | h.Write(this.data) 30 | 31 | this.data = h.Sum(nil) 32 | 33 | return this 34 | } 35 | 36 | // NewLSH256_224 37 | func (this Hash) NewLSH256_224() Hash { 38 | this.hash = lsh256.New224() 39 | 40 | return this 41 | } 42 | -------------------------------------------------------------------------------- /ascon/mac_test.go: -------------------------------------------------------------------------------- 1 | package ascon 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func TestMAC(t *testing.T) { 9 | want := "EB1AF688825D66BF2D53E135F9323315" 10 | mk := func(n int) []byte { 11 | b := make([]byte, n) 12 | for i := range b { 13 | b[i] = byte(i % 256) 14 | } 15 | return b 16 | } 17 | m := NewMAC(mk(16)) 18 | got := fmt.Sprintf("%X", m.Sum(nil)) 19 | if got != want { 20 | t.Errorf("got %s, want %s", got, want) 21 | } 22 | 23 | if ok := m.Verify(fromHex(want)); ok != true { 24 | t.Errorf("Verify(mac) = %t, want %t", ok, true) 25 | } 26 | if ok := m.Verify(mk(16)); ok != false { 27 | t.Errorf("Verify(bad mac) = %t, want %t", ok, false) 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /hash/blake256.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "github.com/deatil/go-hash/blake256" 5 | ) 6 | 7 | // Blake256 hash 8 | func (this Hash) Blake256() Hash { 9 | h := blake256.New() 10 | h.Write(this.data) 11 | 12 | this.data = h.Sum(nil) 13 | 14 | return this 15 | } 16 | 17 | // NewBlake256 18 | func (this Hash) NewBlake256() Hash { 19 | this.hash = blake256.New() 20 | 21 | return this 22 | } 23 | 24 | // =========== 25 | 26 | // Blake224 hash 27 | func (this Hash) Blake224() Hash { 28 | h := blake256.New224() 29 | h.Write(this.data) 30 | 31 | this.data = h.Sum(nil) 32 | 33 | return this 34 | } 35 | 36 | // NewBlake224 37 | func (this Hash) NewBlake224() Hash { 38 | this.hash = blake256.New224() 39 | 40 | return this 41 | } 42 | -------------------------------------------------------------------------------- /hash/blake512.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "github.com/deatil/go-hash/blake512" 5 | ) 6 | 7 | // Blake512 hash 8 | func (this Hash) Blake512() Hash { 9 | h := blake512.New() 10 | h.Write(this.data) 11 | 12 | this.data = h.Sum(nil) 13 | 14 | return this 15 | } 16 | 17 | // NewBlake512 18 | func (this Hash) NewBlake512() Hash { 19 | this.hash = blake512.New() 20 | 21 | return this 22 | } 23 | 24 | // =========== 25 | 26 | // Blake384 hash 27 | func (this Hash) Blake384() Hash { 28 | h := blake512.New384() 29 | h.Write(this.data) 30 | 31 | this.data = h.Sum(nil) 32 | 33 | return this 34 | } 35 | 36 | // NewBlake384 37 | func (this Hash) NewBlake384() Hash { 38 | this.hash = blake512.New384() 39 | 40 | return this 41 | } 42 | -------------------------------------------------------------------------------- /hash/hashing/esch/esch256.go: -------------------------------------------------------------------------------- 1 | package esch 2 | 3 | import ( 4 | go_hash "hash" 5 | 6 | "github.com/deatil/go-hash/hash" 7 | "github.com/deatil/go-hash/esch" 8 | ) 9 | 10 | type ESCH256Hash struct {} 11 | 12 | // 编码 13 | func (this ESCH256Hash) Sum(data []byte, cfg ...any) ([]byte, error) { 14 | sum := esch.Sum256(data) 15 | 16 | return sum[:], nil 17 | } 18 | 19 | // 解码 20 | func (this ESCH256Hash) New(cfg ...any) (go_hash.Hash, error) { 21 | return esch.New256(), nil 22 | } 23 | 24 | var ESCH256 = hash.TypeMode.Generate() 25 | 26 | func init() { 27 | hash.TypeMode.Names().Add(ESCH256, func() string { 28 | return "ESCH256" 29 | }) 30 | 31 | hash.UseHash.Add(ESCH256, func() hash.IHash { 32 | return ESCH256Hash{} 33 | }) 34 | } 35 | -------------------------------------------------------------------------------- /hash/hashing/esch/esch384.go: -------------------------------------------------------------------------------- 1 | package esch 2 | 3 | import ( 4 | go_hash "hash" 5 | 6 | "github.com/deatil/go-hash/hash" 7 | "github.com/deatil/go-hash/esch" 8 | ) 9 | 10 | type ESCH384Hash struct {} 11 | 12 | // 编码 13 | func (this ESCH384Hash) Sum(data []byte, cfg ...any) ([]byte, error) { 14 | sum := esch.Sum384(data) 15 | 16 | return sum[:], nil 17 | } 18 | 19 | // 解码 20 | func (this ESCH384Hash) New(cfg ...any) (go_hash.Hash, error) { 21 | return esch.New384(), nil 22 | } 23 | 24 | var ESCH384 = hash.TypeMode.Generate() 25 | 26 | func init() { 27 | hash.TypeMode.Names().Add(ESCH384, func() string { 28 | return "ESCH384" 29 | }) 30 | 31 | hash.UseHash.Add(ESCH384, func() hash.IHash { 32 | return ESCH384Hash{} 33 | }) 34 | } 35 | -------------------------------------------------------------------------------- /skein/skein512_amd64.go: -------------------------------------------------------------------------------- 1 | // +build amd64 2 | 3 | package skein 4 | 5 | import "unsafe" 6 | 7 | func bytesToBlock(block *[8]uint64, src []byte) { 8 | srcPtr := (*[8]uint64)(unsafe.Pointer(&src[0])) 9 | 10 | block[0] = srcPtr[0] 11 | block[1] = srcPtr[1] 12 | block[2] = srcPtr[2] 13 | block[3] = srcPtr[3] 14 | block[4] = srcPtr[4] 15 | block[5] = srcPtr[5] 16 | block[6] = srcPtr[6] 17 | block[7] = srcPtr[7] 18 | } 19 | 20 | func blockToBytes(dst []byte, block *[8]uint64) { 21 | dstPtr := (*[8]uint64)(unsafe.Pointer(&dst[0])) 22 | 23 | dstPtr[0] = block[0] 24 | dstPtr[1] = block[1] 25 | dstPtr[2] = block[2] 26 | dstPtr[3] = block[3] 27 | dstPtr[4] = block[4] 28 | dstPtr[5] = block[5] 29 | dstPtr[6] = block[6] 30 | dstPtr[7] = block[7] 31 | } 32 | -------------------------------------------------------------------------------- /hash/xxhash.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "github.com/deatil/go-hash/xxhash/xxhash32" 5 | "github.com/deatil/go-hash/xxhash/xxhash64" 6 | ) 7 | 8 | // xxhash32 签名 9 | func (this Hash) Xxhash32() Hash { 10 | h := xxhash32.New() 11 | h.Write(this.data) 12 | 13 | this.data = h.Sum(nil) 14 | 15 | return this 16 | } 17 | 18 | // NewXxhash32 19 | func (this Hash) NewXxhash32() Hash { 20 | this.hash = xxhash32.New() 21 | 22 | return this 23 | } 24 | 25 | // ======== 26 | 27 | // xxhash64 签名 28 | func (this Hash) Xxhash64() Hash { 29 | h := xxhash64.New() 30 | h.Write(this.data) 31 | 32 | this.data = h.Sum(nil) 33 | 34 | return this 35 | } 36 | 37 | // NewXxhash64 38 | func (this Hash) NewXxhash64() Hash { 39 | this.hash = xxhash64.New() 40 | 41 | return this 42 | } 43 | -------------------------------------------------------------------------------- /gost/gost34112012256/kdf.go: -------------------------------------------------------------------------------- 1 | package gost34112012256 2 | 3 | import ( 4 | "hash" 5 | "crypto/hmac" 6 | ) 7 | 8 | func KDF(h func() hash.Hash, key, label, seed []byte) (r []byte) { 9 | fn := hmac.New(h, key) 10 | 11 | if _, err := fn.Write([]byte{0x01}); err != nil { 12 | panic(err) 13 | } 14 | if _, err := fn.Write(label); err != nil { 15 | panic(err) 16 | } 17 | 18 | if _, err := fn.Write([]byte{0x00}); err != nil { 19 | panic(err) 20 | } 21 | if _, err := fn.Write(seed); err != nil { 22 | panic(err) 23 | } 24 | 25 | if _, err := fn.Write([]byte{0x01}); err != nil { 26 | panic(err) 27 | } 28 | if _, err := fn.Write([]byte{0x00}); err != nil { 29 | panic(err) 30 | } 31 | 32 | r = fn.Sum(nil) 33 | 34 | return r 35 | } 36 | -------------------------------------------------------------------------------- /haraka/utils.go: -------------------------------------------------------------------------------- 1 | package haraka 2 | 3 | import ( 4 | "encoding/binary" 5 | ) 6 | 7 | func GETU32(ptr []byte) uint32 { 8 | return binary.LittleEndian.Uint32(ptr) 9 | } 10 | 11 | func PUTU32(ptr []byte, a uint32) { 12 | binary.LittleEndian.PutUint32(ptr, a) 13 | } 14 | 15 | func bytesToUint32s(b []byte) []uint32 { 16 | size := len(b) / 4 17 | dst := make([]uint32, size) 18 | 19 | for i := 0; i < size; i++ { 20 | j := i * 4 21 | 22 | dst[i] = binary.LittleEndian.Uint32(b[j:]) 23 | } 24 | 25 | return dst 26 | } 27 | 28 | func uint32sToBytes(w []uint32) []byte { 29 | size := len(w) * 4 30 | dst := make([]byte, size) 31 | 32 | for i := 0; i < len(w); i++ { 33 | j := i * 4 34 | 35 | binary.LittleEndian.PutUint32(dst[j:], w[i]) 36 | } 37 | 38 | return dst 39 | } 40 | -------------------------------------------------------------------------------- /kupyna/utils.go: -------------------------------------------------------------------------------- 1 | package kupyna 2 | 3 | import ( 4 | "encoding/binary" 5 | ) 6 | 7 | func GETU64(ptr []byte) uint64 { 8 | return binary.LittleEndian.Uint64(ptr) 9 | } 10 | 11 | func PUTU64(ptr []byte, a uint64) { 12 | binary.LittleEndian.PutUint64(ptr, a) 13 | } 14 | 15 | func bytesToUint64s(b []byte) []uint64 { 16 | size := len(b) / 8 17 | dst := make([]uint64, size) 18 | 19 | for i := 0; i < size; i++ { 20 | j := i * 8 21 | 22 | dst[i] = binary.LittleEndian.Uint64(b[j:]) 23 | } 24 | 25 | return dst 26 | } 27 | 28 | func uint64sToBytes(w []uint64) []byte { 29 | size := len(w) * 8 30 | dst := make([]byte, size) 31 | 32 | for i := 0; i < len(w); i++ { 33 | j := i * 8 34 | 35 | binary.LittleEndian.PutUint64(dst[j:], w[i]) 36 | } 37 | 38 | return dst 39 | } 40 | -------------------------------------------------------------------------------- /panama/utils.go: -------------------------------------------------------------------------------- 1 | package panama 2 | 3 | import ( 4 | "encoding/binary" 5 | ) 6 | 7 | func getu32(ptr []byte) uint32 { 8 | return binary.LittleEndian.Uint32(ptr) 9 | } 10 | 11 | func putu32(ptr []byte, a uint32) { 12 | binary.LittleEndian.PutUint32(ptr, a) 13 | } 14 | 15 | func bytesToUint32s(b []byte) []uint32 { 16 | size := len(b) / 4 17 | dst := make([]uint32, size) 18 | 19 | for i := 0; i < size; i++ { 20 | j := i * 4 21 | 22 | dst[i] = binary.LittleEndian.Uint32(b[j:]) 23 | } 24 | 25 | return dst 26 | } 27 | 28 | func uint32sToBytes(w []uint32) []byte { 29 | size := len(w) * 4 30 | dst := make([]byte, size) 31 | 32 | for i := 0; i < len(w); i++ { 33 | j := i * 4 34 | 35 | binary.LittleEndian.PutUint32(dst[j:], w[i]) 36 | } 37 | 38 | return dst 39 | } 40 | -------------------------------------------------------------------------------- /hash/keccak.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "golang.org/x/crypto/sha3" 5 | ) 6 | 7 | // Keccak256 哈希值 8 | func (this Hash) Keccak256() Hash { 9 | h := sha3.NewLegacyKeccak256() 10 | h.Write(this.data) 11 | 12 | this.data = h.Sum(nil) 13 | 14 | return this 15 | } 16 | 17 | // NewKeccak256 18 | func (this Hash) NewKeccak256() Hash { 19 | this.hash = sha3.NewLegacyKeccak256() 20 | 21 | return this 22 | } 23 | 24 | // ============================================ 25 | 26 | // Keccak512 哈希值 27 | func (this Hash) Keccak512() Hash { 28 | h := sha3.NewLegacyKeccak512() 29 | h.Write(this.data) 30 | 31 | this.data = h.Sum(nil) 32 | 33 | return this 34 | } 35 | 36 | // NewKeccak512 37 | func (this Hash) NewKeccak512() Hash { 38 | this.hash = sha3.NewLegacyKeccak512() 39 | 40 | return this 41 | } 42 | -------------------------------------------------------------------------------- /hash/tiger_test.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func Test_Tiger(t *testing.T) { 9 | in := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" 10 | check := "8dcea680a17583ee502ba38a3c368651890ffbccdc49a8cc" 11 | 12 | res := FromString(in).Tiger().ToBytes() 13 | 14 | if fmt.Sprintf("%x", res) != check { 15 | t.Errorf("Check Hash error, got %x, want %s", res, check) 16 | } 17 | } 18 | 19 | func Test_Tiger2(t *testing.T) { 20 | in := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" 21 | check := "ea9ab6228cee7b51b77544fca6066c8cbb5bbae6319505cd" 22 | 23 | res := FromString(in).Tiger2().ToBytes() 24 | 25 | if fmt.Sprintf("%x", res) != check { 26 | t.Errorf("Check Hash error, got %x, want %s", res, check) 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /radio_gatun/radio_gatun.go: -------------------------------------------------------------------------------- 1 | package radio_gatun 2 | 3 | import ( 4 | "hash" 5 | ) 6 | 7 | // New32 returns a new hash.Hash computing the RadioGatun-32 checksum 8 | func New32() hash.Hash { 9 | return newDigest32() 10 | } 11 | 12 | // New64 returns a new hash.Hash computing the RadioGatun-64 checksum 13 | func New64() hash.Hash { 14 | return newDigest64() 15 | } 16 | 17 | // Sum32 returns the RadioGatun-32 checksum of the data. 18 | func Sum32(data []byte) (out [Size32]byte) { 19 | h := New32() 20 | h.Write(data) 21 | sum := h.Sum(nil) 22 | 23 | copy(out[:], sum) 24 | return 25 | } 26 | 27 | // Sum64 returns the RadioGatun-64 checksum of the data. 28 | func Sum64(data []byte) (out [Size64]byte) { 29 | h := New64() 30 | h.Write(data) 31 | sum := h.Sum(nil) 32 | 33 | copy(out[:], sum) 34 | return 35 | } 36 | -------------------------------------------------------------------------------- /skein/skein512_ref.go: -------------------------------------------------------------------------------- 1 | // +build !amd64 2 | 3 | package skein 4 | 5 | func bytesToBlock(block *[8]uint64, src []byte) { 6 | for i := range block { 7 | j := i * 8 8 | block[i] = uint64(src[j]) | uint64(src[j+1])<<8 | uint64(src[j+2])<<16 | 9 | uint64(src[j+3])<<24 | uint64(src[j+4])<<32 | uint64(src[j+5])<<40 | 10 | uint64(src[j+6])<<48 | uint64(src[j+7])<<56 11 | } 12 | } 13 | 14 | func blockToBytes(dst []byte, block *[8]uint64) { 15 | i := 0 16 | for _, v := range block { 17 | dst[i] = byte(v) 18 | dst[i+1] = byte(v >> 8) 19 | dst[i+2] = byte(v >> 16) 20 | dst[i+3] = byte(v >> 24) 21 | dst[i+4] = byte(v >> 32) 22 | dst[i+5] = byte(v >> 40) 23 | dst[i+6] = byte(v >> 48) 24 | dst[i+7] = byte(v >> 56) 25 | i += 8 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /siphash/utils.go: -------------------------------------------------------------------------------- 1 | package siphash 2 | 3 | import ( 4 | "math/bits" 5 | "encoding/binary" 6 | ) 7 | 8 | func GETU64(ptr []byte) uint64 { 9 | return binary.LittleEndian.Uint64(ptr) 10 | } 11 | 12 | func PUTU64(ptr []byte, a uint64) { 13 | binary.LittleEndian.PutUint64(ptr, a) 14 | } 15 | 16 | func ROTL(x, n uint64) uint64 { 17 | return bits.RotateLeft64(x, int(n)) 18 | } 19 | 20 | func sipround(v0, v1, v2, v3 *uint64) { 21 | (*v0) += (*v1) 22 | (*v1) = ROTL((*v1), 13) 23 | 24 | (*v1) ^= (*v0) 25 | (*v0) = ROTL((*v0), 32) 26 | 27 | (*v2) += (*v3) 28 | (*v3) = ROTL((*v3), 16) 29 | 30 | (*v3) ^= (*v2) 31 | (*v0) += (*v3) 32 | (*v3) = ROTL((*v3), 21) 33 | 34 | (*v3) ^= (*v0) 35 | (*v2) += (*v1) 36 | (*v1) = ROTL((*v1), 17) 37 | 38 | (*v1) ^= (*v2) 39 | (*v2) = ROTL((*v2), 32) 40 | } 41 | -------------------------------------------------------------------------------- /haraka/haraka.go: -------------------------------------------------------------------------------- 1 | package haraka 2 | 3 | var hasAES = false 4 | 5 | // Haraka256 calculates the Harakaa v2 hash of a 256-bit 6 | // input and places the 256-bit result in output. 7 | func Haraka256(input [32]byte) (output [32]byte) { 8 | if hasAES { 9 | haraka256AES(&rc[0], &output[0], &input[0]) 10 | } else { 11 | haraka256Ref(&output, &input) 12 | } 13 | 14 | return 15 | } 16 | 17 | // Haraka512 calculates the Harakaa v2 hash of a 512-bit 18 | // input and places the 256-bit result in output. 19 | func Haraka512(input [64]byte) (output [32]byte) { 20 | if hasAES { 21 | haraka512AES(&rc[0], &output[0], &input[0]) 22 | } else { 23 | haraka512Ref(&output, &input) 24 | } 25 | 26 | return 27 | } 28 | 29 | func haraka256AES(rc *uint32, dst, src *byte) 30 | func haraka512AES(rc *uint32, dst, src *byte) 31 | -------------------------------------------------------------------------------- /skein/skein256/skein256_ref.go: -------------------------------------------------------------------------------- 1 | // +build !amd64 2 | 3 | package skein256 4 | 5 | func bytesToBlock(block *[4]uint64, src []byte) { 6 | for i := range block { 7 | j := i * 8 8 | block[i] = uint64(src[j]) | uint64(src[j+1])<<8 | uint64(src[j+2])<<16 | 9 | uint64(src[j+3])<<24 | uint64(src[j+4])<<32 | uint64(src[j+5])<<40 | 10 | uint64(src[j+6])<<48 | uint64(src[j+7])<<56 11 | } 12 | } 13 | 14 | func blockToBytes(dst []byte, block *[4]uint64) { 15 | i := 0 16 | for _, v := range block { 17 | dst[i] = byte(v) 18 | dst[i+1] = byte(v >> 8) 19 | dst[i+2] = byte(v >> 16) 20 | dst[i+3] = byte(v >> 24) 21 | dst[i+4] = byte(v >> 32) 22 | dst[i+5] = byte(v >> 40) 23 | dst[i+6] = byte(v >> 48) 24 | dst[i+7] = byte(v >> 56) 25 | i += 8 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /skein/skein1024/skein1024_ref.go: -------------------------------------------------------------------------------- 1 | // +build !amd64 2 | 3 | package skein1024 4 | 5 | func bytesToBlock(block *[16]uint64, src []byte) { 6 | for i := range block { 7 | j := i * 8 8 | block[i] = uint64(src[j]) | uint64(src[j+1])<<8 | uint64(src[j+2])<<16 | 9 | uint64(src[j+3])<<24 | uint64(src[j+4])<<32 | uint64(src[j+5])<<40 | 10 | uint64(src[j+6])<<48 | uint64(src[j+7])<<56 11 | } 12 | } 13 | 14 | func blockToBytes(dst []byte, block *[16]uint64) { 15 | i := 0 16 | for _, v := range block { 17 | dst[i] = byte(v) 18 | dst[i+1] = byte(v >> 8) 19 | dst[i+2] = byte(v >> 16) 20 | dst[i+3] = byte(v >> 24) 21 | dst[i+4] = byte(v >> 32) 22 | dst[i+5] = byte(v >> 40) 23 | dst[i+6] = byte(v >> 48) 24 | dst[i+7] = byte(v >> 56) 25 | i += 8 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /cmac/verify.go: -------------------------------------------------------------------------------- 1 | package cmac 2 | 3 | import ( 4 | "crypto/cipher" 5 | "crypto/subtle" 6 | ) 7 | 8 | // Sum computes the CMAC checksum with the given tagsize of msg using the cipher.Block. 9 | func Sum(msg []byte, c cipher.Block, tagsize int) ([]byte, error) { 10 | h, err := NewWithTagSize(c, tagsize) 11 | if err != nil { 12 | return nil, err 13 | } 14 | 15 | h.Write(msg) 16 | return h.Sum(nil), nil 17 | } 18 | 19 | // Verify computes the CMAC checksum with the given tagsize of msg and compares 20 | // it with the given mac. This functions returns true if and only if the given mac 21 | // is equal to the computed one. 22 | func Verify(mac, msg []byte, c cipher.Block, tagsize int) bool { 23 | sum, err := Sum(msg, c, tagsize) 24 | if err != nil { 25 | return false 26 | } 27 | 28 | return subtle.ConstantTimeCompare(mac, sum) == 1 29 | } 30 | -------------------------------------------------------------------------------- /pmac/verify.go: -------------------------------------------------------------------------------- 1 | package pmac 2 | 3 | import ( 4 | "crypto/cipher" 5 | "crypto/subtle" 6 | ) 7 | 8 | // Sum computes the PMAC checksum with the given tagsize of msg using the cipher.Block. 9 | func Sum(msg []byte, c cipher.Block, tagsize int) ([]byte, error) { 10 | h, err := NewWithTagSize(c, tagsize) 11 | if err != nil { 12 | return nil, err 13 | } 14 | 15 | h.Write(msg) 16 | return h.Sum(nil), nil 17 | } 18 | 19 | // Verify computes the PMAC checksum with the given tagsize of msg and compares 20 | // it with the given mac. This functions returns true if and only if the given mac 21 | // is equal to the computed one. 22 | func Verify(mac, msg []byte, c cipher.Block, tagsize int) bool { 23 | sum, err := Sum(msg, c, tagsize) 24 | if err != nil { 25 | return false 26 | } 27 | 28 | return subtle.ConstantTimeCompare(mac, sum) == 1 29 | } 30 | -------------------------------------------------------------------------------- /rabin/poly_test.go: -------------------------------------------------------------------------------- 1 | package rabin 2 | 3 | import "testing" 4 | 5 | func TestPolyMod(t *testing.T) { 6 | var p polyGF2 7 | for _, test := range [][3]uint64{ 8 | {0x3e75, 0x3e75, 0x0}, // a(x) mod a(x) = 0 9 | {0x3e75 << 1, 0x3e75, 0x0}, // a(x)*x mod a(x) = 0 10 | {0x3e74, 0x3e75, 0x1}, // a(x) + 1 mod a(x) = 1 11 | {0x7337, 0xe39b, 0x7337}, // degree(a) < degree(b) 12 | // Random polynomials, checked with Wolfram Alpha. 13 | {0x3e75, 0x201b, 0x1e6e}, 14 | {0xd10b, 0x35f7, 0x6d7}, 15 | {0xe5a2, 0x8c83, 0x6921}, 16 | {0x9a4a, 0xa8c7, 0x328d}, 17 | } { 18 | a, b := newPolyGF2(test[0]), newPolyGF2(test[1]) 19 | p.Mod(a, b) 20 | if p.coeff.Uint64() != test[2] { 21 | t.Errorf("%s mod %s = %s (%#x), want %s", a, b, &p, p.coeff.Uint64(), newPolyGF2(test[2])) 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /md6/sbox.go: -------------------------------------------------------------------------------- 1 | package md6 2 | 3 | const b = 512 4 | const c = 128 5 | const n = 89 6 | 7 | var S0 = []uint32{0x01234567, 0x89ABCDEF} 8 | var Sm = []uint32{0x7311C281, 0x2425CFA0} 9 | 10 | var Q = [][]uint32{ 11 | {0x7311C281, 0x2425CFA0}, 12 | {0x64322864, 0x34AAC8E7}, 13 | {0xB60450E9, 0xEF68B7C1}, 14 | {0xE8FB2390, 0x8D9F06F1}, 15 | {0xDD2E76CB, 0xA691E5BF}, 16 | {0x0CD0D63B, 0x2C30BC41}, 17 | {0x1F8CCF68, 0x23058F8A}, 18 | {0x54E5ED5B, 0x88E3775D}, 19 | {0x4AD12AAE, 0x0A6D6031}, 20 | {0x3E7F16BB, 0x88222E0D}, 21 | {0x8AF8671D, 0x3FB50C2C}, 22 | {0x995AD117, 0x8BD25C31}, 23 | {0xC878C1DD, 0x04C4B633}, 24 | {0x3B72066C, 0x7A1552AC}, 25 | {0x0D6F3522, 0x631EFFCB}, 26 | } 27 | 28 | var t = []int{17, 18, 21, 31, 67, 89} 29 | var rs = []int{10, 5, 13, 10, 11, 12, 2, 7, 14, 15, 7, 13, 11, 7, 6, 12} 30 | var ls = []int{11, 24, 9, 16, 15, 9, 27, 15, 6, 2, 29, 8, 15, 5, 31, 9} 31 | 32 | -------------------------------------------------------------------------------- /echo/utils.go: -------------------------------------------------------------------------------- 1 | package echo 2 | 3 | import ( 4 | "encoding/binary" 5 | ) 6 | 7 | func GETU64(ptr []byte) uint64 { 8 | return binary.LittleEndian.Uint64(ptr) 9 | } 10 | 11 | func PUTU64(ptr []byte, a uint64) { 12 | binary.LittleEndian.PutUint64(ptr, a) 13 | } 14 | 15 | func PUTU16(ptr []byte, a uint16) { 16 | binary.LittleEndian.PutUint16(ptr, a) 17 | } 18 | 19 | func bytesToUint64s(b []byte) []uint64 { 20 | size := len(b) / 8 21 | dst := make([]uint64, size) 22 | 23 | for i := 0; i < size; i++ { 24 | j := i * 8 25 | 26 | dst[i] = binary.LittleEndian.Uint64(b[j:]) 27 | } 28 | 29 | return dst 30 | } 31 | 32 | func uint64sToBytes(w []uint64) []byte { 33 | size := len(w) * 8 34 | dst := make([]byte, size) 35 | 36 | for i := 0; i < len(w); i++ { 37 | j := i * 8 38 | 39 | binary.LittleEndian.PutUint64(dst[j:], w[i]) 40 | } 41 | 42 | return dst 43 | } 44 | -------------------------------------------------------------------------------- /lyra2re/sum.go: -------------------------------------------------------------------------------- 1 | package lyra2re 2 | 3 | import ( 4 | "golang.org/x/crypto/sha3" 5 | 6 | "github.com/deatil/go-hash/skein" 7 | "github.com/deatil/go-hash/groestl" 8 | "github.com/deatil/go-hash/blake256" 9 | ) 10 | 11 | // Sum returns the result of Lyra2re hash. 12 | func Sum(data []byte) ([]byte, error) { 13 | blake := blake256.New() 14 | if _, err := blake.Write(data); err != nil { 15 | return nil, err 16 | } 17 | 18 | resultBlake := blake.Sum(nil) 19 | 20 | keccak := sha3.NewLegacyKeccak256() 21 | if _, err := keccak.Write(resultBlake); err != nil { 22 | return nil, err 23 | } 24 | resultKeccak := keccak.Sum(nil) 25 | 26 | lyra2Result := make([]byte, 32) 27 | Lyra2(lyra2Result, resultKeccak, resultKeccak, 1, 8, 8) 28 | 29 | skeinResult := skein.Sum256(lyra2Result, nil) 30 | 31 | groestlResult := groestl.Sum256(skeinResult[:]) 32 | 33 | return groestlResult[:], nil 34 | } 35 | -------------------------------------------------------------------------------- /skeins/sbox.go: -------------------------------------------------------------------------------- 1 | package skeins 2 | 3 | var initVal224 = [8]uint64{ 4 | 0xCCD0616248677224, 0xCBA65CF3A92339EF, 5 | 0x8CCD69D652FF4B64, 0x398AED7B3AB890B4, 6 | 0x0F59D1B1457D2BD0, 0x6776FE6575D4EB3D, 7 | 0x99FBC70E997413E9, 0x9E2CFCCFE1C41EF7, 8 | } 9 | 10 | var initVal256 = [8]uint64{ 11 | 0xCCD044A12FDB3E13, 0xE83590301A79A9EB, 12 | 0x55AEA0614F816E6F, 0x2A2767A4AE9B94DB, 13 | 0xEC06025E74DD7683, 0xE7A436CDC4746251, 14 | 0xC36FBAF9393AD185, 0x3EEDBA1833EDFC13, 15 | } 16 | 17 | var initVal384 = [8]uint64{ 18 | 0xA3F6C6BF3A75EF5F, 0xB0FEF9CCFD84FAA4, 19 | 0x9D77DD663D770CFE, 0xD798CBF3B468FDDA, 20 | 0x1BC4A6668A0E4465, 0x7ED7D434E5807407, 21 | 0x548FC1ACD4EC44D6, 0x266E17546AA18FF8, 22 | } 23 | 24 | var initVal512 = [8]uint64{ 25 | 0x4903ADFF749C51CE, 0x0D95DE399746DF03, 26 | 0x8FD1934127C79BCE, 0x9A255629FF352CB1, 27 | 0x5DB62599DF6CA7B0, 0xEABE394CA9D5C3F4, 28 | 0x991112C71A75B523, 0xAE18A40B660FCC33, 29 | } 30 | -------------------------------------------------------------------------------- /hash/crc64.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "hash/crc64" 5 | ) 6 | 7 | // CRC64ISO 8 | func (this Hash) CRC64_ISO() Hash { 9 | tab := crc64.MakeTable(crc64.ISO) 10 | 11 | h := crc64.New(tab) 12 | h.Write(this.data) 13 | 14 | this.data = h.Sum(nil) 15 | 16 | return this 17 | } 18 | 19 | // NewCRC64_ISO 20 | func (this Hash) NewCRC64_ISO() Hash { 21 | tab := crc64.MakeTable(crc64.ISO) 22 | 23 | this.hash = crc64.New(tab) 24 | 25 | return this 26 | } 27 | 28 | // ============================================ 29 | 30 | // CRC64ECMA 哈希值 31 | func (this Hash) CRC64_ECMA() Hash { 32 | tab := crc64.MakeTable(crc64.ECMA) 33 | 34 | h := crc64.New(tab) 35 | h.Write(this.data) 36 | 37 | this.data = h.Sum(nil) 38 | 39 | return this 40 | } 41 | 42 | // NewCRC64_ECMA 43 | func (this Hash) NewCRC64_ECMA() Hash { 44 | tab := crc64.MakeTable(crc64.ECMA) 45 | 46 | this.hash = crc64.New(tab) 47 | 48 | return this 49 | } 50 | -------------------------------------------------------------------------------- /jh2/utils.go: -------------------------------------------------------------------------------- 1 | package jh2 2 | 3 | import ( 4 | "encoding/binary" 5 | ) 6 | 7 | func getu32(ptr []byte) uint32 { 8 | return binary.BigEndian.Uint32(ptr) 9 | } 10 | 11 | func putu32(ptr []byte, a uint32) { 12 | binary.BigEndian.PutUint32(ptr, a) 13 | } 14 | 15 | func getu64(ptr []byte) uint64 { 16 | return binary.BigEndian.Uint64(ptr) 17 | } 18 | 19 | func putu64(ptr []byte, a uint64) { 20 | binary.BigEndian.PutUint64(ptr, a) 21 | } 22 | 23 | func bytesToUint32s(b []byte) []uint32 { 24 | size := len(b) / 4 25 | dst := make([]uint32, size) 26 | 27 | for i := 0; i < size; i++ { 28 | j := i * 4 29 | 30 | dst[i] = binary.BigEndian.Uint32(b[j:]) 31 | } 32 | 33 | return dst 34 | } 35 | 36 | func uint32sToBytes(w []uint32) []byte { 37 | size := len(w) * 4 38 | dst := make([]byte, size) 39 | 40 | for i := 0; i < len(w); i++ { 41 | j := i * 4 42 | 43 | binary.BigEndian.PutUint32(dst[j:], w[i]) 44 | } 45 | 46 | return dst 47 | } 48 | -------------------------------------------------------------------------------- /tiger/tiger.go: -------------------------------------------------------------------------------- 1 | package tiger 2 | 3 | import ( 4 | "hash" 5 | ) 6 | 7 | // New returns a new hash.Hash computing the Tiger hash value 8 | func New() hash.Hash { 9 | return newDigest(Size192, 1) 10 | } 11 | 12 | // New2 returns a new hash.Hash computing the Tiger2 hash value 13 | func New2() hash.Hash { 14 | return newDigest(Size192, 2) 15 | } 16 | 17 | // =========== 18 | 19 | // New160 returns a new hash.Hash computing the Tiger160 checksum 20 | func New160() hash.Hash { 21 | return newDigest(Size160, 1) 22 | } 23 | 24 | // New2_160 returns a new hash.Hash computing the Tiger2_160 checksum 25 | func New2_160() hash.Hash { 26 | return newDigest(Size160, 2) 27 | } 28 | 29 | // =========== 30 | 31 | // New128 returns a new hash.Hash computing the Tiger128 checksum 32 | func New128() hash.Hash { 33 | return newDigest(Size128, 1) 34 | } 35 | 36 | // New2_128 returns a new hash.Hash computing the Tiger2_128 checksum 37 | func New2_128() hash.Hash { 38 | return newDigest(Size128, 2) 39 | } 40 | -------------------------------------------------------------------------------- /luffa/utils.go: -------------------------------------------------------------------------------- 1 | package luffa 2 | 3 | import ( 4 | "encoding/binary" 5 | ) 6 | 7 | func getu32(ptr []byte) uint32 { 8 | return binary.BigEndian.Uint32(ptr) 9 | } 10 | 11 | func putu32(ptr []byte, a uint32) { 12 | binary.BigEndian.PutUint32(ptr, a) 13 | } 14 | 15 | func getu64(ptr []byte) uint64 { 16 | return binary.BigEndian.Uint64(ptr) 17 | } 18 | 19 | func putu64(ptr []byte, a uint64) { 20 | binary.BigEndian.PutUint64(ptr, a) 21 | } 22 | 23 | func bytesToUint32s(b []byte) []uint32 { 24 | size := len(b) / 4 25 | dst := make([]uint32, size) 26 | 27 | for i := 0; i < size; i++ { 28 | j := i * 4 29 | 30 | dst[i] = binary.BigEndian.Uint32(b[j:]) 31 | } 32 | 33 | return dst 34 | } 35 | 36 | func uint32sToBytes(w []uint32) []byte { 37 | size := len(w) * 4 38 | dst := make([]byte, size) 39 | 40 | for i := 0; i < len(w); i++ { 41 | j := i * 4 42 | 43 | binary.BigEndian.PutUint32(dst[j:], w[i]) 44 | } 45 | 46 | return dst 47 | } 48 | -------------------------------------------------------------------------------- /sha0/utils.go: -------------------------------------------------------------------------------- 1 | package sha0 2 | 3 | import ( 4 | "encoding/binary" 5 | ) 6 | 7 | func getu32(ptr []byte) uint32 { 8 | return binary.BigEndian.Uint32(ptr) 9 | } 10 | 11 | func putu32(ptr []byte, a uint32) { 12 | binary.BigEndian.PutUint32(ptr, a) 13 | } 14 | 15 | func getu64(ptr []byte) uint64 { 16 | return binary.BigEndian.Uint64(ptr) 17 | } 18 | 19 | func putu64(ptr []byte, a uint64) { 20 | binary.BigEndian.PutUint64(ptr, a) 21 | } 22 | 23 | func bytesToUint32s(b []byte) []uint32 { 24 | size := len(b) / 4 25 | dst := make([]uint32, size) 26 | 27 | for i := 0; i < size; i++ { 28 | j := i * 4 29 | 30 | dst[i] = binary.BigEndian.Uint32(b[j:]) 31 | } 32 | 33 | return dst 34 | } 35 | 36 | func uint32sToBytes(w []uint32) []byte { 37 | size := len(w) * 4 38 | dst := make([]byte, size) 39 | 40 | for i := 0; i < len(w); i++ { 41 | j := i * 4 42 | 43 | binary.BigEndian.PutUint32(dst[j:], w[i]) 44 | } 45 | 46 | return dst 47 | } 48 | -------------------------------------------------------------------------------- /fsb/utils.go: -------------------------------------------------------------------------------- 1 | package fsb 2 | 3 | import ( 4 | "encoding/binary" 5 | ) 6 | 7 | // Convert a byte slice to a uint32 slice 8 | func bytesToUints(b []byte) []uint32 { 9 | size := len(b) / 4 10 | dst := make([]uint32, size) 11 | 12 | for i := 0; i < size; i++ { 13 | j := i * 4 14 | 15 | dst[i] = binary.BigEndian.Uint32(b[j:]) 16 | } 17 | 18 | return dst 19 | } 20 | 21 | // Convert a uint32 slice to a byte slice 22 | func uintsToBytes(w []uint32) []byte { 23 | size := len(w) * 4 24 | dst := make([]byte, size) 25 | 26 | for i := 0; i < len(w); i++ { 27 | j := i * 4 28 | 29 | binary.BigEndian.PutUint32(dst[j:], w[i]) 30 | } 31 | 32 | return dst 33 | } 34 | 35 | func LUI(a uint32) int { 36 | return int(a - 1) / (4 << 3) + 1 37 | } 38 | 39 | func logarithm(a uint32) int { 40 | var i int 41 | for i = 0; i < 32; i++ { 42 | if a == uint32(1 << i) { 43 | return i 44 | } 45 | } 46 | 47 | return -1 48 | } 49 | 50 | -------------------------------------------------------------------------------- /k12/k12.go: -------------------------------------------------------------------------------- 1 | package k12 2 | 3 | // This is an implementation of KangarooTwelve 4 | // at http://keccak.noekeon.org/kangarootwelve.html. 5 | 6 | // The constructor requires a customization string. 7 | func New(customString []byte) *digest { 8 | return newDigest(customString, 32) 9 | } 10 | 11 | // Sum returns the k12 checksum of the data. 12 | func Sum(customString []byte, data []byte) (out [32]byte) { 13 | h := New(customString) 14 | h.Write(data) 15 | sum := h.Sum(nil) 16 | 17 | copy(out[:], sum) 18 | return 19 | } 20 | 21 | // =========== 22 | 23 | // The constructor requires a customization string. 24 | func NewWithSize(customString []byte, hashSize int) *digest { 25 | return newDigest(customString, hashSize) 26 | } 27 | 28 | // SumWithSize returns the k12 checksum of the data. 29 | func SumWithSize(customString []byte, data []byte, hashSize int) (out []byte) { 30 | h := NewWithSize(customString, hashSize) 31 | h.Write(data) 32 | sum := h.Sum(nil) 33 | 34 | return sum 35 | } 36 | -------------------------------------------------------------------------------- /kupyna/kupyna384.go: -------------------------------------------------------------------------------- 1 | package kupyna 2 | 3 | import ( 4 | "hash" 5 | ) 6 | 7 | // The size of an kupyna384 checksum in bytes. 8 | const Size384 = 48 9 | 10 | // The blocksize of kupyna384 in bytes. 11 | const BlockSize384 = 128 12 | 13 | // digest384 represents the partial evaluation of a checksum. 14 | type digest384 struct { 15 | h hash.Hash 16 | } 17 | 18 | // New384 returns a new hash.Hash computing the Kupyna384 checksum 19 | func New384() hash.Hash { 20 | d := new(digest384) 21 | d.h = New512() 22 | d.Reset() 23 | 24 | return d 25 | } 26 | 27 | func (d *digest384) Reset() { 28 | d.h.Reset() 29 | } 30 | 31 | func (d *digest384) Size() int { 32 | return Size384 33 | } 34 | 35 | func (d *digest384) BlockSize() int { 36 | return BlockSize384 37 | } 38 | 39 | func (d *digest384) Write(p []byte) (nn int, err error) { 40 | return d.h.Write(p) 41 | } 42 | 43 | func (d *digest384) Sum(in []byte) []byte { 44 | hash := d.h.Sum(nil) 45 | return append(in, hash[16:]...) 46 | } 47 | -------------------------------------------------------------------------------- /xxhash/xxhash32/xxhash32.go: -------------------------------------------------------------------------------- 1 | package xxhash32 2 | 3 | import "hash" 4 | 5 | // New returns a new Hash32 instance. 6 | func New() hash.Hash32 { 7 | return newDigest(0) 8 | } 9 | 10 | // NewWithSeed returns a new Hash32 instance. 11 | func NewWithSeed(seed uint32) hash.Hash32 { 12 | return newDigest(seed) 13 | } 14 | 15 | // Checksum returns the 32bits Hash value. 16 | func Sum(input []byte) (out [Size]byte) { 17 | sum := checksum(input, 0) 18 | putu32be(out[:], sum) 19 | 20 | return 21 | } 22 | 23 | // Checksum returns the 32bits Hash value. 24 | func SumWithSeed(input []byte, seed uint32) (out [Size]byte) { 25 | sum := checksum(input, seed) 26 | putu32be(out[:], sum) 27 | 28 | return 29 | } 30 | 31 | // Checksum returns the 32bits Hash value. 32 | func Checksum(input []byte) uint32 { 33 | return checksum(input, 0) 34 | } 35 | 36 | // ChecksumWithSeed returns the 32bits Hash value. 37 | func ChecksumWithSeed(input []byte, seed uint32) uint32 { 38 | return checksum(input, seed) 39 | } 40 | -------------------------------------------------------------------------------- /xxhash/xxhash64/xxhash64.go: -------------------------------------------------------------------------------- 1 | package xxhash64 2 | 3 | import "hash" 4 | 5 | // New returns a new Hash64 instance. 6 | func New() hash.Hash64 { 7 | return newDigest(0) 8 | } 9 | 10 | // NewWithSeed returns a new Hash64 instance. 11 | func NewWithSeed(seed uint64) hash.Hash64 { 12 | return newDigest(seed) 13 | } 14 | 15 | // Checksum returns the 64bits Hash value. 16 | func Sum(input []byte) (out [Size]byte) { 17 | sum := checksum(input, 0) 18 | putu64be(out[:], sum) 19 | 20 | return 21 | } 22 | 23 | // Checksum returns the 64bits Hash value. 24 | func SumWithSeed(input []byte, seed uint64) (out [Size]byte) { 25 | sum := checksum(input, seed) 26 | putu64be(out[:], sum) 27 | 28 | return 29 | } 30 | 31 | // Checksum returns the 64bits Hash value. 32 | func Checksum(input []byte) uint64 { 33 | return checksum(input, 0) 34 | } 35 | 36 | // ChecksumWithSeed returns the 64bits Hash value. 37 | func ChecksumWithSeed(input []byte, seed uint64) uint64 { 38 | return checksum(input, seed) 39 | } 40 | -------------------------------------------------------------------------------- /shabal/utils.go: -------------------------------------------------------------------------------- 1 | package shabal 2 | 3 | import ( 4 | "encoding/binary" 5 | ) 6 | 7 | func getu32(ptr []byte) uint32 { 8 | return binary.LittleEndian.Uint32(ptr) 9 | } 10 | 11 | func putu32(ptr []byte, a uint32) { 12 | binary.LittleEndian.PutUint32(ptr, a) 13 | } 14 | 15 | func getu64(ptr []byte) uint64 { 16 | return binary.LittleEndian.Uint64(ptr) 17 | } 18 | 19 | func putu64(ptr []byte, a uint64) { 20 | binary.LittleEndian.PutUint64(ptr, a) 21 | } 22 | 23 | func bytesToUint32s(b []byte) []uint32 { 24 | size := len(b) / 4 25 | dst := make([]uint32, size) 26 | 27 | for i := 0; i < size; i++ { 28 | j := i * 4 29 | 30 | dst[i] = binary.LittleEndian.Uint32(b[j:]) 31 | } 32 | 33 | return dst 34 | } 35 | 36 | func uint32sToBytes(w []uint32) []byte { 37 | size := len(w) * 4 38 | dst := make([]byte, size) 39 | 40 | for i := 0; i < len(w); i++ { 41 | j := i * 4 42 | 43 | binary.LittleEndian.PutUint32(dst[j:], w[i]) 44 | } 45 | 46 | return dst 47 | } 48 | -------------------------------------------------------------------------------- /shavite/utils.go: -------------------------------------------------------------------------------- 1 | package shavite 2 | 3 | import ( 4 | "encoding/binary" 5 | ) 6 | 7 | func getu32(ptr []byte) uint32 { 8 | return binary.LittleEndian.Uint32(ptr) 9 | } 10 | 11 | func putu32(ptr []byte, a uint32) { 12 | binary.LittleEndian.PutUint32(ptr, a) 13 | } 14 | 15 | func getu64(ptr []byte) uint64 { 16 | return binary.LittleEndian.Uint64(ptr) 17 | } 18 | 19 | func putu64(ptr []byte, a uint64) { 20 | binary.LittleEndian.PutUint64(ptr, a) 21 | } 22 | 23 | func bytesToUint32s(b []byte) []uint32 { 24 | size := len(b) / 4 25 | dst := make([]uint32, size) 26 | 27 | for i := 0; i < size; i++ { 28 | j := i * 4 29 | 30 | dst[i] = binary.LittleEndian.Uint32(b[j:]) 31 | } 32 | 33 | return dst 34 | } 35 | 36 | func uint32sToBytes(w []uint32) []byte { 37 | size := len(w) * 4 38 | dst := make([]byte, size) 39 | 40 | for i := 0; i < len(w); i++ { 41 | j := i * 4 42 | 43 | binary.LittleEndian.PutUint32(dst[j:], w[i]) 44 | } 45 | 46 | return dst 47 | } 48 | -------------------------------------------------------------------------------- /bash/sbox.go: -------------------------------------------------------------------------------- 1 | package bash 2 | 3 | var bash_rc = [BASH_ROUNDS]uint64{ 4 | 0x3bf5080ac8ba94b1, 5 | 0xc1d1659c1bbd92f6, 6 | 0x60e8b2ce0ddec97b, 7 | 0xec5fb8fe790fbc13, 8 | 0xaa043de6436706a7, 9 | 0x8929ff6a5e535bfd, 10 | 0x98bf1e2c50c97550, 11 | 0x4c5f8f162864baa8, 12 | 0x262fc78b14325d54, 13 | 0x1317e3c58a192eaa, 14 | 0x098bf1e2c50c9755, 15 | 0xd8ee19681d669304, 16 | 0x6c770cb40eb34982, 17 | 0x363b865a0759a4c1, 18 | 0xc73622b47c4c0ace, 19 | 0x639b115a3e260567, 20 | 0xede6693460f3da1d, 21 | 0xaad8d5034f9935a0, 22 | 0x556c6a81a7cc9ad0, 23 | 0x2ab63540d3e64d68, 24 | 0x155b1aa069f326b4, 25 | 0x0aad8d5034f9935a, 26 | 0x0556c6a81a7cc9ad, 27 | 0xde8082cd72debc78, 28 | } 29 | 30 | var bash_rot = [BASH_ROT_ROUNDS][BASH_ROT_IDX]byte{ 31 | { 8, 53, 14, 1 }, 32 | { 56, 51, 34, 7 }, 33 | { 8, 37, 46, 49 }, 34 | { 56, 3, 2, 23 }, 35 | { 8, 21, 14, 33 }, 36 | { 56, 19, 34, 39 }, 37 | { 8, 5, 46, 17 }, 38 | { 56, 35, 2, 55 }, 39 | } 40 | -------------------------------------------------------------------------------- /radio_gatun/utils.go: -------------------------------------------------------------------------------- 1 | package radio_gatun 2 | 3 | import ( 4 | "encoding/binary" 5 | ) 6 | 7 | func getu32(ptr []byte) uint32 { 8 | return binary.LittleEndian.Uint32(ptr) 9 | } 10 | 11 | func putu32(ptr []byte, a uint32) { 12 | binary.LittleEndian.PutUint32(ptr, a) 13 | } 14 | 15 | func getu64(ptr []byte) uint64 { 16 | return binary.LittleEndian.Uint64(ptr) 17 | } 18 | 19 | func putu64(ptr []byte, a uint64) { 20 | binary.LittleEndian.PutUint64(ptr, a) 21 | } 22 | 23 | func bytesToUint32s(b []byte) []uint32 { 24 | size := len(b) / 4 25 | dst := make([]uint32, size) 26 | 27 | for i := 0; i < size; i++ { 28 | j := i * 4 29 | 30 | dst[i] = binary.LittleEndian.Uint32(b[j:]) 31 | } 32 | 33 | return dst 34 | } 35 | 36 | func uint32sToBytes(w []uint32) []byte { 37 | size := len(w) * 4 38 | dst := make([]byte, size) 39 | 40 | for i := 0; i < len(w); i++ { 41 | j := i * 4 42 | 43 | binary.LittleEndian.PutUint32(dst[j:], w[i]) 44 | } 45 | 46 | return dst 47 | } 48 | -------------------------------------------------------------------------------- /streebog/streebog.go: -------------------------------------------------------------------------------- 1 | package streebog 2 | 3 | import ( 4 | "hash" 5 | ) 6 | 7 | // New returns a new hash.Hash computing the Streebog checksum 8 | func New(hashsize int) (hash.Hash, error) { 9 | return newDigest(hashsize) 10 | } 11 | 12 | // New256 returns a new hash.Hash computing the Streebog checksum 13 | func New256() hash.Hash { 14 | h, _ := New(256) 15 | return h 16 | } 17 | 18 | // New512 returns a new hash.Hash computing the Streebog checksum 19 | func New512() hash.Hash { 20 | h, _ := New(512) 21 | return h 22 | } 23 | 24 | // Sum256 returns the Streebog-256 checksum of the data. 25 | func Sum256(data []byte) (sum256 [Size256]byte) { 26 | h := New256() 27 | h.Write(data) 28 | sum := h.Sum(nil) 29 | 30 | copy(sum256[:], sum[:Size256]) 31 | return 32 | } 33 | 34 | // Sum512 returns the Streebog-512 checksum of the data. 35 | func Sum512(data []byte) (sum512 [Size512]byte) { 36 | h := New512() 37 | h.Write(data) 38 | sum := h.Sum(nil) 39 | 40 | copy(sum512[:], sum[:Size512]) 41 | return 42 | } 43 | -------------------------------------------------------------------------------- /vmac/sbox.go: -------------------------------------------------------------------------------- 1 | package vmac 2 | 3 | import ( 4 | "math/big" 5 | ) 6 | 7 | const ( 8 | l1keylen = 1024 9 | l1keysize = l1keylen / 8 10 | ) 11 | 12 | var ( 13 | one = big.NewInt(1) 14 | m64 = new(big.Int).Lsh(one, 64) // 2^64 15 | m126 = new(big.Int).Lsh(one, 126) // 2^126 16 | m128 = new(big.Int).Lsh(one, 128) // 2^128 17 | p64 = bytesToBigint([]byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFF}) // 2^64 - 257 18 | p64p32 = bytesToBigint([]byte{0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00}) // 2^64 - 2^32 19 | p127 = bytesToBigint([]byte{0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}) // 2^127 - 1 20 | ) 21 | -------------------------------------------------------------------------------- /lyra2re/lyra2re_test.go: -------------------------------------------------------------------------------- 1 | package lyra2re 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func TestLyra2(t *testing.T) { 9 | tests := []struct { 10 | name string 11 | in []byte 12 | out string 13 | }{ 14 | { 15 | name: "Basic", 16 | in: []byte{0x34, 0x6e, 0x80, 0x88, 0x0e, 0xcc, 0x9e, 0x84, 0xce, 0x60, 0x22, 0xcf, 0x37, 0x56, 0xa1, 0xdf, 0x17, 0x56, 0x84, 0x0e, 0xf7, 0xea, 0x65, 0xc6, 0x44, 0xc9, 0x9f, 0x6d, 0x3d, 0xa3, 0x1f, 0x2b}, 17 | out: "c0c4bf26d8411f3bff3fb4b66e2570ab8305b336f0afdcf6bcf84be9a8eedecc", 18 | }, 19 | } 20 | 21 | for _, test := range tests { 22 | test := test 23 | t.Run(test.name, func(t *testing.T) { 24 | var sum [32]byte 25 | Lyra2(sum[:], test.in, test.in, 1, 8, 8) 26 | 27 | got := fmt.Sprintf("%x", sum) 28 | if got != test.out { 29 | t.Errorf("Expected %q", test.out) 30 | t.Errorf("Got %q", got) 31 | } 32 | }) 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /lyra2re2/lyra2re_test.go: -------------------------------------------------------------------------------- 1 | package lyra2re2 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func Test_Lyra2RE(t *testing.T) { 9 | tests := []struct { 10 | name string 11 | in []byte 12 | out string 13 | }{ 14 | { 15 | name: "Basic", 16 | in: []byte{0x34, 0x6e, 0x80, 0x88, 0x0e, 0xcc, 0x9e, 0x84, 0xce, 0x60, 0x22, 0xcf, 0x37, 0x56, 0xa1, 0xdf, 0x17, 0x56, 0x84, 0x0e, 0xf7, 0xea, 0x65, 0xc6, 0x44, 0xc9, 0x9f, 0x6d, 0x3d, 0xa3, 0x1f, 0x2b}, 17 | out: "dbea2100f29a25f83cb31257cb61e0abbf30095dc2e96315abbec24b835f8d56", 18 | }, 19 | } 20 | 21 | for _, test := range tests { 22 | test := test 23 | t.Run(test.name, func(t *testing.T) { 24 | var sum [32]byte 25 | Lyra2(sum[:], test.in, test.in, 1, 8, 8) 26 | 27 | got := fmt.Sprintf("%x", sum) 28 | if got != test.out { 29 | t.Errorf("Expected %q", test.out) 30 | t.Errorf("Got %q", got) 31 | } 32 | }) 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /esch/esch.go: -------------------------------------------------------------------------------- 1 | package esch 2 | 3 | import ( 4 | "hash" 5 | ) 6 | 7 | // New returns a new hash.Hash computing the ESCH checksum 8 | func New(hashsize int) (hash.Hash, error) { 9 | return newDigest(hashsize) 10 | } 11 | 12 | // =========== 13 | 14 | // New256 returns a new hash.Hash computing the ESCH checksum 15 | func New256() hash.Hash { 16 | h, _ := New(256) 17 | return h 18 | } 19 | 20 | // Sum256 returns the ESCH-256 checksum of the data. 21 | func Sum256(data []byte) (sum256 [Size256]byte) { 22 | h := New256() 23 | h.Write(data) 24 | sum := h.Sum(nil) 25 | 26 | copy(sum256[:], sum[:Size256]) 27 | return 28 | } 29 | 30 | // =========== 31 | 32 | // New384 returns a new hash.Hash computing the ESCH checksum 33 | func New384() hash.Hash { 34 | h, _ := New(384) 35 | return h 36 | } 37 | 38 | // Sum384 returns the ESCH-384 checksum of the data. 39 | func Sum384(data []byte) (sum384 [Size384]byte) { 40 | h := New384() 41 | h.Write(data) 42 | sum := h.Sum(nil) 43 | 44 | copy(sum384[:], sum[:Size384]) 45 | return 46 | } 47 | -------------------------------------------------------------------------------- /gost/gost34112012/hash_test.go: -------------------------------------------------------------------------------- 1 | package gost34112012 2 | 3 | import ( 4 | "fmt" 5 | "hash" 6 | "encoding" 7 | "testing" 8 | ) 9 | 10 | func TestHashInterface(t *testing.T) { 11 | h := New(64) 12 | var _ hash.Hash = h 13 | var _ encoding.BinaryMarshaler = h 14 | var _ encoding.BinaryUnmarshaler = h 15 | } 16 | 17 | func TestHashed(t *testing.T) { 18 | h := New(64) 19 | m := make([]byte, BlockSize) 20 | for i := 0; i < BlockSize; i++ { 21 | m[i] = byte(i) 22 | } 23 | h.Write(m) 24 | hashed := h.Sum(nil) 25 | 26 | if len(hashed) == 0 { 27 | t.Error("Hash error") 28 | } 29 | } 30 | 31 | func Test_Check(t *testing.T) { 32 | in := []byte("nonce-asdfg") 33 | check := "56d6dd148d3df5947b54f0a0fb5e5b0234680cd7b4614bf3005c86fffb45257419b3133c39e551347cd3ad26850bd9513877ee2b708829f3f8f902377720655f" 34 | 35 | h := New(64) 36 | h.Write(in) 37 | 38 | out := h.Sum(nil) 39 | 40 | if fmt.Sprintf("%x", out) != check { 41 | t.Errorf("Check error. got %x, want %s", out, check) 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /lsh256/lsh256.go: -------------------------------------------------------------------------------- 1 | package lsh256 2 | 3 | import ( 4 | "hash" 5 | ) 6 | 7 | const ( 8 | // The size of a LSH-256 checksum in bytes. 9 | Size = 32 10 | 11 | // The size of a LSH-224 checksum in bytes. 12 | Size224 = 28 13 | 14 | // The blocksize of LSH-256 and LSH-224 in bytes. 15 | BlockSize = 128 16 | ) 17 | 18 | // New returns a new hash.Hash computing the LSH-256 checksum. 19 | func New() hash.Hash { 20 | return newDigest(Size) 21 | } 22 | 23 | // New224 returns a new hash.Hash computing the LSH-224 checksum. 24 | func New224() hash.Hash { 25 | return newDigest(Size224) 26 | } 27 | 28 | // Sum256 returns the LSH-256 checksum of the data. 29 | func Sum256(data []byte) (sum256 [Size]byte) { 30 | b := New() 31 | b.Write(data) 32 | sum := b.Sum(nil) 33 | 34 | copy(sum256[:], sum) 35 | return 36 | } 37 | 38 | // Sum224 returns the LSH-224 checksum of the data. 39 | func Sum224(data []byte) (sum224 [Size224]byte) { 40 | b := New224() 41 | b.Write(data) 42 | sum := b.Sum(nil) 43 | 44 | copy(sum224[:], sum) 45 | return 46 | } 47 | -------------------------------------------------------------------------------- /md6/md6.go: -------------------------------------------------------------------------------- 1 | package md6 2 | 3 | import ( 4 | "hash" 5 | ) 6 | 7 | // New224 returns a new hash.Hash computing the MD6 checksum. 8 | func New224() hash.Hash { 9 | return New(224) 10 | } 11 | 12 | // New256 returns a new hash.Hash computing the MD6 checksum. 13 | func New256() hash.Hash { 14 | return New(256) 15 | } 16 | 17 | // New384 returns a new hash.Hash computing the MD6 checksum. 18 | func New384() hash.Hash { 19 | return New(384) 20 | } 21 | 22 | // New512 returns a new hash.Hash computing the MD6 checksum. 23 | func New512() hash.Hash { 24 | return New(512) 25 | } 26 | 27 | // New returns a new hash.Hash computing the MD6 checksum. 28 | func New(size int) hash.Hash { 29 | return newDigest(size, nil, 64) 30 | } 31 | 32 | // NewWithKey returns a new hash.Hash computing the MD6 checksum. 33 | func NewWithKey(size int, key []byte) hash.Hash { 34 | return newDigest(size, key, 64) 35 | } 36 | 37 | // NewMD6 returns a new hash.Hash computing the MD6 checksum. 38 | func NewMD6(size int, key []byte, levels int) hash.Hash { 39 | return newDigest(size, key, levels) 40 | } 41 | -------------------------------------------------------------------------------- /sm3/sbox.go: -------------------------------------------------------------------------------- 1 | package sm3 2 | 3 | var iv = [8]uint32{ 4 | 0x7380166F, 0x4914B2B9, 0x172442D7, 0xDA8A0600, 5 | 0xA96F30BC, 0x163138AA, 0xE38DEE4D, 0xB0FB0E4E, 6 | } 7 | 8 | var tSbox = [64]uint32{ 9 | 0x79cc4519, 0xf3988a32, 0xe7311465, 0xce6228cb, 10 | 0x9cc45197, 0x3988a32f, 0x7311465e, 0xe6228cbc, 11 | 0xcc451979, 0x988a32f3, 0x311465e7, 0x6228cbce, 12 | 0xc451979c, 0x88a32f39, 0x11465e73, 0x228cbce6, 13 | 0x9d8a7a87, 0x3b14f50f, 0x7629ea1e, 0xec53d43c, 14 | 0xd8a7a879, 0xb14f50f3, 0x629ea1e7, 0xc53d43ce, 15 | 0x8a7a879d, 0x14f50f3b, 0x29ea1e76, 0x53d43cec, 16 | 0xa7a879d8, 0x4f50f3b1, 0x9ea1e762, 0x3d43cec5, 17 | 0x7a879d8a, 0xf50f3b14, 0xea1e7629, 0xd43cec53, 18 | 0xa879d8a7, 0x50f3b14f, 0xa1e7629e, 0x43cec53d, 19 | 0x879d8a7a, 0x0f3b14f5, 0x1e7629ea, 0x3cec53d4, 20 | 0x79d8a7a8, 0xf3b14f50, 0xe7629ea1, 0xcec53d43, 21 | 0x9d8a7a87, 0x3b14f50f, 0x7629ea1e, 0xec53d43c, 22 | 0xd8a7a879, 0xb14f50f3, 0x629ea1e7, 0xc53d43ce, 23 | 0x8a7a879d, 0x14f50f3b, 0x29ea1e76, 0x53d43cec, 24 | 0xa7a879d8, 0x4f50f3b1, 0x9ea1e762, 0x3d43cec5, 25 | } 26 | -------------------------------------------------------------------------------- /lyra2re/sum_test.go: -------------------------------------------------------------------------------- 1 | package lyra2re 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func Test_SumV1(t *testing.T) { 9 | tests := []struct { 10 | name string 11 | in []byte 12 | out string 13 | }{ 14 | { 15 | name: "Basic", 16 | in: []byte{0x34, 0x6e, 0x80, 0x88, 0x0e, 0xcc, 0x9e, 0x84, 0xce, 0x60, 0x22, 0xcf, 0x37, 0x56, 0xa1, 0xdf, 0x17, 0x56, 0x84, 0x0e, 0xf7, 0xea, 0x65, 0xc6, 0x44, 0xc9, 0x9f, 0x6d, 0x3d, 0xa3, 0x1f, 0x2b}, 17 | out: "52e973e71f42c73a1adea7f149ef8c070b24f48c6a03b7877babd1fa761ba3ee", 18 | }, 19 | } 20 | 21 | for _, test := range tests { 22 | test := test 23 | t.Run(test.name, func(t *testing.T) { 24 | sum, err := Sum(test.in) 25 | if err != nil { 26 | t.Errorf("Error while summing: %v", err) 27 | } 28 | 29 | got := fmt.Sprintf("%x", sum) 30 | if got != test.out { 31 | t.Errorf("not match, got %x, want %s", sum, test.out) 32 | } 33 | }) 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /blake3/sbox.go: -------------------------------------------------------------------------------- 1 | package blake3 2 | 3 | const BLAKE3_KEY_LEN = 32 4 | const BLAKE3_OUT_LEN = 32 5 | const BLAKE3_BLOCK_LEN = 64 6 | const BLAKE3_CHUNK_LEN = 1024 7 | const BLAKE3_MAX_DEPTH = 54 8 | 9 | const ( 10 | CHUNK_START = 1 << 0 11 | CHUNK_END = 1 << 1 12 | PARENT = 1 << 2 13 | ROOT = 1 << 3 14 | KEYED_HASH = 1 << 4 15 | DERIVE_KEY_CONTEXT = 1 << 5 16 | DERIVE_KEY_MATERIAL = 1 << 6 17 | ) 18 | 19 | var iv = [8]uint32{ 20 | 0x6A09E667, 0xBB67AE85, 0x3C6EF372, 21 | 0xA54FF53A, 0x510E527F, 0x9B05688C, 22 | 0x1F83D9AB, 0x5BE0CD19, 23 | } 24 | 25 | var MSG_SCHEDULE = [7][16]uint32{ 26 | {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, 27 | {2, 6, 3, 10, 7, 0, 4, 13, 1, 11, 12, 5, 9, 14, 15, 8}, 28 | {3, 4, 10, 12, 13, 2, 7, 14, 6, 5, 9, 0, 11, 15, 8, 1}, 29 | {10, 7, 12, 9, 14, 3, 13, 15, 4, 0, 11, 2, 5, 8, 1, 6}, 30 | {12, 13, 9, 11, 15, 10, 14, 8, 7, 2, 5, 3, 0, 1, 6, 4}, 31 | {9, 14, 11, 5, 8, 12, 15, 1, 13, 3, 0, 10, 2, 6, 4, 7}, 32 | {11, 15, 5, 0, 1, 9, 8, 6, 14, 10, 2, 12, 3, 4, 7, 13}, 33 | } 34 | -------------------------------------------------------------------------------- /k12/xor.go: -------------------------------------------------------------------------------- 1 | // Copyright 2015 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package k12 6 | 7 | import ( 8 | "crypto/subtle" 9 | "encoding/binary" 10 | "unsafe" 11 | 12 | "golang.org/x/sys/cpu" 13 | ) 14 | 15 | // xorIn xors the bytes in buf into the state. 16 | func xorIn(d *state, buf []byte) { 17 | if cpu.IsBigEndian { 18 | for i := 0; len(buf) >= 8; i++ { 19 | a := binary.LittleEndian.Uint64(buf) 20 | d.a[i] ^= a 21 | buf = buf[8:] 22 | } 23 | } else { 24 | ab := (*[25 * 64 / 8]byte)(unsafe.Pointer(&d.a)) 25 | subtle.XORBytes(ab[:], ab[:], buf) 26 | } 27 | } 28 | 29 | // copyOut copies uint64s to a byte buffer. 30 | func copyOut(d *state, b []byte) { 31 | if cpu.IsBigEndian { 32 | for i := 0; len(b) >= 8; i++ { 33 | binary.LittleEndian.PutUint64(b, d.a[i]) 34 | b = b[8:] 35 | } 36 | } else { 37 | ab := (*[25 * 64 / 8]byte)(unsafe.Pointer(&d.a)) 38 | copy(b, ab[:]) 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /siphash/siphash.go: -------------------------------------------------------------------------------- 1 | package siphash 2 | 3 | import ( 4 | "hash" 5 | ) 6 | 7 | // New returns a new hash.Hash computing the Siphash checksum. 8 | func New(k []byte) hash.Hash { 9 | h, _ := NewWithCDroundsAndHashSize(k, 0, 0, 0) 10 | return h 11 | } 12 | 13 | // New64 returns a new hash.Hash computing the Siphash checksum. 14 | func New64(k []byte) hash.Hash { 15 | h, _ := NewWithCDroundsAndHashSize(k, 0, 0, HashSize64) 16 | return h 17 | } 18 | 19 | // New128 returns a new hash.Hash computing the Siphash checksum. 20 | func New128(k []byte) hash.Hash { 21 | h, _ := NewWithCDroundsAndHashSize(k, 0, 0, HashSize128) 22 | return h 23 | } 24 | 25 | // NewWithHashSize returns a new hash.Hash computing the Siphash checksum. 26 | func NewWithHashSize(k []byte, hashSize int) hash.Hash { 27 | h, _ := NewWithCDroundsAndHashSize(k, 0, 0, hashSize) 28 | return h 29 | } 30 | 31 | // NewWithCDroundsAndHashSize returns a new hash.Hash computing the Siphash checksum. 32 | func NewWithCDroundsAndHashSize(k []byte, crounds, drounds int32, hashSize int) (hash.Hash, error) { 33 | return newDigest(k, crounds, drounds, hashSize) 34 | } 35 | -------------------------------------------------------------------------------- /md2/binary_test.go: -------------------------------------------------------------------------------- 1 | package md2 2 | 3 | import ( 4 | "fmt" 5 | "bytes" 6 | "testing" 7 | ) 8 | 9 | func Test_MarshalBinary(t *testing.T) { 10 | msg := []byte("abcdefghijklmnopqrstuvwxyz") 11 | check := "4e8ddff3650292ab5a4108c3aa47940b" 12 | 13 | h := newDigest() 14 | h.Write(msg) 15 | dst := h.Sum(nil) 16 | if len(dst) == 0 { 17 | t.Error("Hash make error") 18 | } 19 | 20 | bs, _ := h.MarshalBinary() 21 | 22 | h2 := newDigest() 23 | err := h2.UnmarshalBinary(bs) 24 | if err != nil { 25 | t.Fatal(err) 26 | } 27 | 28 | newdst := h2.Sum(nil) 29 | if len(newdst) == 0 { 30 | t.Error("newHash make error") 31 | } 32 | 33 | if !bytes.Equal(newdst, dst) { 34 | t.Errorf("Hash MarshalBinary error, got %x, want %x", newdst, dst) 35 | } 36 | 37 | // =========== 38 | 39 | sum1 := fmt.Sprintf("%x", dst) 40 | if sum1 != check { 41 | t.Errorf("Sum error, got %s, want %s", sum1, check) 42 | } 43 | 44 | sum2 := fmt.Sprintf("%x", newdst) 45 | if sum2 != check { 46 | t.Errorf("ReSum error, got %s, want %s", sum2, check) 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /hash_composition/digest.go: -------------------------------------------------------------------------------- 1 | package hash_composition 2 | 3 | import ( 4 | "hash" 5 | ) 6 | 7 | // digest represents the partial evaluation of a checksum. 8 | type digest struct { 9 | h1 hash.Hash 10 | h2 hash.Hash 11 | } 12 | 13 | // newDigest returns a new digest computing the hash_composition checksum 14 | func newDigest(h1, h2 func() hash.Hash) *digest { 15 | d := new(digest) 16 | d.h1 = h1() 17 | d.h2 = h2() 18 | d.Reset() 19 | 20 | return d 21 | } 22 | 23 | func (d *digest) Reset() { 24 | d.h1.Reset() 25 | d.h2.Reset() 26 | } 27 | 28 | func (d *digest) Size() int { 29 | return d.h1.Size() 30 | } 31 | 32 | func (d *digest) BlockSize() int { 33 | return d.h1.BlockSize() 34 | } 35 | 36 | func (d *digest) Write(p []byte) (nn int, err error) { 37 | return d.h2.Write(p) 38 | } 39 | 40 | func (d *digest) Sum(in []byte) []byte { 41 | // Make a copy of d so that caller can keep writing and summing. 42 | d0 := *d 43 | hash := d0.checkSum() 44 | return append(in, hash...) 45 | } 46 | 47 | func (d *digest) checkSum() []byte { 48 | h2Digest := d.h2.Sum(nil) 49 | 50 | d.h1.Write(h2Digest) 51 | return d.h1.Sum(nil) 52 | } 53 | -------------------------------------------------------------------------------- /hamsi/utils.go: -------------------------------------------------------------------------------- 1 | package hamsi 2 | 3 | import ( 4 | "math/bits" 5 | "encoding/binary" 6 | ) 7 | 8 | func getu32(ptr []byte) uint32 { 9 | return binary.BigEndian.Uint32(ptr) 10 | } 11 | 12 | func putu32(ptr []byte, a uint32) { 13 | binary.BigEndian.PutUint32(ptr, a) 14 | } 15 | 16 | func getu64(ptr []byte) uint64 { 17 | return binary.BigEndian.Uint64(ptr) 18 | } 19 | 20 | func putu64(ptr []byte, a uint64) { 21 | binary.BigEndian.PutUint64(ptr, a) 22 | } 23 | 24 | func bytesToUint32s(b []byte) []uint32 { 25 | size := len(b) / 4 26 | dst := make([]uint32, size) 27 | 28 | for i := 0; i < size; i++ { 29 | j := i * 4 30 | 31 | dst[i] = binary.BigEndian.Uint32(b[j:]) 32 | } 33 | 34 | return dst 35 | } 36 | 37 | func uint32sToBytes(w []uint32) []byte { 38 | size := len(w) * 4 39 | dst := make([]byte, size) 40 | 41 | for i := 0; i < len(w); i++ { 42 | j := i * 4 43 | 44 | binary.BigEndian.PutUint32(dst[j:], w[i]) 45 | } 46 | 47 | return dst 48 | } 49 | 50 | func rol(x uint32, n int) uint32 { 51 | return bits.RotateLeft32(x, n) 52 | } 53 | 54 | func ror(x uint32, n int) uint32 { 55 | return rol(x, 32 - n) 56 | } 57 | -------------------------------------------------------------------------------- /gmac/gmac_test.go: -------------------------------------------------------------------------------- 1 | package gmac 2 | 3 | import ( 4 | "bytes" 5 | "testing" 6 | "crypto/aes" 7 | "encoding/hex" 8 | ) 9 | 10 | func fromHex(s string) []byte { 11 | h, _ := hex.DecodeString(s) 12 | return h 13 | } 14 | 15 | func Test_GMAC_Check(t *testing.T) { 16 | // https://cryptopp.com/wiki/GMAC 17 | var ( 18 | key = fromHex(`00000000000000000000000000000000`) 19 | iv = fromHex(`00000000000000000000000000000000`) 20 | msg = []byte(`Yoda said, do or do not. There is no try.`) 21 | expect = fromHex(`E7EE2C63B4DC328EED4A86B3FB3490AF`) 22 | ) 23 | 24 | c, err := aes.NewCipher(key) 25 | if err != nil { 26 | t.Fatal(err) 27 | } 28 | 29 | h, err := New(c, iv) 30 | if err != nil { 31 | t.Fatal(err) 32 | } 33 | 34 | h.Write(msg) 35 | actual := h.Sum(nil) 36 | if !bytes.Equal(actual, expect) { 37 | t.Errorf("want: %x, got: %x", expect, actual) 38 | return 39 | } 40 | 41 | h.Reset() 42 | h.Write(msg) 43 | actual = h.Sum(actual[:0]) 44 | if !bytes.Equal(actual, expect) { 45 | t.Errorf("want: %x, got: %x", expect, actual) 46 | return 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /lyra2re2/sum.go: -------------------------------------------------------------------------------- 1 | package lyra2re2 2 | 3 | import ( 4 | "golang.org/x/crypto/sha3" 5 | 6 | "github.com/deatil/go-hash/bmw" 7 | "github.com/deatil/go-hash/skein" 8 | "github.com/deatil/go-hash/blake256" 9 | "github.com/deatil/go-hash/cubehash" 10 | ) 11 | 12 | // Sum returns the result of Lyra2re2 hash. 13 | func Sum(data []byte) ([]byte, error) { 14 | blake := blake256.New() 15 | if _, err := blake.Write(data); err != nil { 16 | return nil, err 17 | } 18 | 19 | resultBlake := blake.Sum(nil) 20 | 21 | keccak := sha3.NewLegacyKeccak256() 22 | if _, err := keccak.Write(resultBlake); err != nil { 23 | return nil, err 24 | } 25 | resultkeccak := keccak.Sum(nil) 26 | 27 | resultcube := cubehash256(resultkeccak) 28 | 29 | lyra2result := make([]byte, 32) 30 | Lyra2(lyra2result, resultcube, resultcube, 1, 4, 4) 31 | 32 | skeinresult := skein.Sum256(lyra2result, nil) 33 | 34 | resultcube2 := cubehash256(skeinresult[:]) 35 | resultbmw := bmw.Sum256(resultcube2) 36 | 37 | return resultbmw[:], nil 38 | } 39 | 40 | func cubehash256(data []byte) []byte { 41 | h := cubehash.NewSH256() 42 | h.Write(data) 43 | return h.Sum(nil) 44 | } 45 | -------------------------------------------------------------------------------- /gost/gost341194/hash_test.go: -------------------------------------------------------------------------------- 1 | package gost341194 2 | 3 | import ( 4 | "fmt" 5 | "hash" 6 | "testing" 7 | "crypto/cipher" 8 | ) 9 | 10 | func TestHashInterface(t *testing.T) { 11 | h := New(NewTestCipher) 12 | var _ hash.Hash = h 13 | } 14 | 15 | func TestVectors(t *testing.T) { 16 | h := New(NewTestCipher) 17 | h.Write([]byte("a")) 18 | hashed := h.Sum(nil) 19 | 20 | if len(hashed) == 0 { 21 | t.Error("Hash error") 22 | } 23 | } 24 | 25 | func NewTestCipher(key []byte) cipher.Block { 26 | return &testCipher{} 27 | } 28 | 29 | type testCipher struct {} 30 | 31 | func (c *testCipher) BlockSize() int { 32 | return 32 33 | } 34 | 35 | func (c *testCipher) Encrypt(dst, src []byte) { 36 | copy(dst, src) 37 | } 38 | 39 | func (c *testCipher) Decrypt(dst, src []byte) { 40 | copy(dst, src) 41 | } 42 | 43 | func Test_Check(t *testing.T) { 44 | in := []byte("nonce-asdfg") 45 | check := "08246810594b02216e6f6e633d2d39733c666352565b5b6a344e580c0b4e580c" 46 | 47 | h := New(NewTestCipher) 48 | h.Write(in) 49 | 50 | out := h.Sum(nil) 51 | 52 | if fmt.Sprintf("%x", out) != check { 53 | t.Errorf("Check error. got %x, want %s", out, check) 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /blake256/utils.go: -------------------------------------------------------------------------------- 1 | package blake256 2 | 3 | import ( 4 | "math/bits" 5 | "encoding/binary" 6 | ) 7 | 8 | func putu64(ptr []byte, a uint64) { 9 | binary.BigEndian.PutUint64(ptr, a) 10 | } 11 | 12 | func bytesToUint32s(b []byte) []uint32 { 13 | size := len(b) / 4 14 | dst := make([]uint32, size) 15 | 16 | for i := 0; i < size; i++ { 17 | j := i * 4 18 | 19 | dst[i] = binary.BigEndian.Uint32(b[j:]) 20 | } 21 | 22 | return dst 23 | } 24 | 25 | func uint32sToBytes(w []uint32) []byte { 26 | size := len(w) * 4 27 | dst := make([]byte, size) 28 | 29 | for i := 0; i < len(w); i++ { 30 | j := i * 4 31 | 32 | binary.BigEndian.PutUint32(dst[j:], w[i]) 33 | } 34 | 35 | return dst 36 | } 37 | 38 | func rotr(x uint32, n int) uint32 { 39 | return bits.RotateLeft32(x, 32 - n) 40 | } 41 | 42 | func G(v *[16]uint32, m []uint32, i int, a, b, c, d, e int) { 43 | v[a] += (m[sigma[i][e]] ^ u256[sigma[i][e+1]]) + v[b] 44 | v[d] = rotr(v[d] ^ v[a], 16) 45 | v[c] += v[d] 46 | v[b] = rotr(v[b] ^ v[c], 12) 47 | v[a] += (m[sigma[i][e+1]] ^ u256[sigma[i][e]])+v[b] 48 | v[d] = rotr(v[d] ^ v[a], 8) 49 | v[c] += v[d] 50 | v[b] = rotr(v[b] ^ v[c], 7) 51 | } 52 | -------------------------------------------------------------------------------- /whirlpool/whirlpool.go: -------------------------------------------------------------------------------- 1 | package whirlpool 2 | 3 | import ( 4 | "hash" 5 | ) 6 | 7 | // New returns a new hash.Hash computing the Whirlpool checksum. 8 | func New() hash.Hash { 9 | return newDigest(c, rc) 10 | } 11 | 12 | // Sum returns the Whirlpool checksum of the data. 13 | func Sum(data []byte) (out [Size]byte) { 14 | h := New() 15 | h.Write(data) 16 | sum := h.Sum(nil) 17 | 18 | copy(out[:], sum) 19 | return 20 | } 21 | 22 | // ======== 23 | 24 | // New0 returns a new hash.Hash computing the Whirlpool-0 checksum. 25 | func New0() hash.Hash { 26 | return newDigest(c0, rc0) 27 | } 28 | 29 | // Sum0 returns the Whirlpool-0 checksum of the data. 30 | func Sum0(data []byte) (out [Size]byte) { 31 | h := New0() 32 | h.Write(data) 33 | sum := h.Sum(nil) 34 | 35 | copy(out[:], sum) 36 | return 37 | } 38 | 39 | // ======== 40 | 41 | // New1 returns a new hash.Hash computing the Whirlpool-1 checksum. 42 | func New1() hash.Hash { 43 | return newDigest(c1, rc1) 44 | } 45 | 46 | // Sum1 returns the Whirlpool-1 checksum of the data. 47 | func Sum1(data []byte) (out [Size]byte) { 48 | h := New1() 49 | h.Write(data) 50 | sum := h.Sum(nil) 51 | 52 | copy(out[:], sum) 53 | return 54 | } 55 | -------------------------------------------------------------------------------- /hash/md4_test.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | var md4Tests = []struct { 9 | input string 10 | output string 11 | }{ 12 | {"sdfgsdgfsdfg123132", "6446daa2729ca846dcd524ce7dabbaa2"}, 13 | {"dfg.;kp[jewijr0-34lsd", "2731537e39ad34cd0dd49997966f0660"}, 14 | {"123123", "4a4a963e47c7b8a3b355e0e0c90d0aa0"}, 15 | } 16 | 17 | func Test_MD4(t *testing.T) { 18 | assert := assertT(t) 19 | assertError := assertErrorT(t) 20 | 21 | for index, test := range md4Tests { 22 | e := FromString(test.input).MD4() 23 | 24 | t.Run(fmt.Sprintf("MD4_test_%d", index), func(t *testing.T) { 25 | assertError(e.Error, "MD4") 26 | assert(test.output, e.ToHexString(), "MD4") 27 | }) 28 | } 29 | } 30 | 31 | func Test_NewMD4(t *testing.T) { 32 | assert := assertT(t) 33 | assertError := assertErrorT(t) 34 | 35 | for index, test := range md4Tests { 36 | e := FromString("").NewMD4().Write([]byte(test.input)).Sum(nil) 37 | 38 | t.Run(fmt.Sprintf("NewMD4_test_%d", index), func(t *testing.T) { 39 | assertError(e.Error, "NewMD4") 40 | assert(test.output, e.ToHexString(), "NewMD4") 41 | }) 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /pmac/block.go: -------------------------------------------------------------------------------- 1 | package pmac 2 | 3 | import ( 4 | "crypto/cipher" 5 | "crypto/subtle" 6 | ) 7 | 8 | const ( 9 | // R is the minimal irreducible polynomial for a 128-bit block size 10 | R = 0x87 11 | ) 12 | 13 | // Block is a 128-bit array used by certain block ciphers (i.e. AES) 14 | type Block struct { 15 | Data []byte 16 | 17 | Size int 18 | } 19 | 20 | func NewBlock(size int) Block { 21 | return Block{ 22 | Data: make([]byte, size), 23 | Size: size, 24 | } 25 | } 26 | 27 | // Clear zeroes out the contents of the block 28 | func (b *Block) Clear() { 29 | for i := range b.Data { 30 | b.Data[i] = 0 31 | } 32 | } 33 | 34 | // Dbl performs a doubling of a block over GF(2^128): 35 | // 36 | // a<<1 if firstbit(a)=0 37 | // (a<<1) ⊕ 0¹²⁰10000111 if firstbit(a)=1 38 | // 39 | func (b *Block) Dbl() { 40 | var z byte 41 | 42 | for i := b.Size - 1; i >= 0; i-- { 43 | zz := b.Data[i] >> 7 44 | b.Data[i] = b.Data[i]<<1 | z 45 | z = zz 46 | } 47 | 48 | b.Data[b.Size-1] ^= byte(subtle.ConstantTimeSelect(int(z), R, 0)) 49 | } 50 | 51 | // Encrypt a block with the given block cipher 52 | func (b *Block) Encrypt(c cipher.Block) { 53 | c.Encrypt(b.Data, b.Data) 54 | } 55 | -------------------------------------------------------------------------------- /blake256/blake256.go: -------------------------------------------------------------------------------- 1 | package blake256 2 | 3 | import "hash" 4 | 5 | // New returns a new hash.Hash computing the BLAKE-256 checksum. 6 | func New() hash.Hash { 7 | return newDigest(256, iv256) 8 | } 9 | 10 | // NewWithSalt is like New but initializes salt with the given 16-byte slice. 11 | func NewWithSalt(salt []byte) hash.Hash { 12 | d := newDigest(256, iv256) 13 | d.setSalt(salt) 14 | return d 15 | } 16 | 17 | // Sum returns the BLAKE-256 checksum of the data. 18 | func Sum(data []byte) (out [Size]byte) { 19 | d := New() 20 | d.Write(data) 21 | sum := d.Sum(nil) 22 | 23 | copy(out[:], sum) 24 | return 25 | } 26 | 27 | // ======= 28 | 29 | // New224 returns a new hash.Hash computing the BLAKE-224 checksum. 30 | func New224() hash.Hash { 31 | return newDigest(224, iv224) 32 | } 33 | 34 | // New224WithSalt is like New224 but initializes salt with the given 16-byte slice. 35 | func New224WithSalt(salt []byte) hash.Hash { 36 | d := newDigest(224, iv224) 37 | d.setSalt(salt) 38 | return d 39 | } 40 | 41 | // Sum224 returns the BLAKE-224 checksum of the data. 42 | func Sum224(data []byte) (out [Size224]byte) { 43 | d := New224() 44 | d.Write(data) 45 | sum := d.Sum(nil) 46 | 47 | copy(out[:], sum) 48 | return 49 | } 50 | -------------------------------------------------------------------------------- /blake512/blake512.go: -------------------------------------------------------------------------------- 1 | package blake512 2 | 3 | import "hash" 4 | 5 | // New returns a new hash.Hash computing the BLAKE-512 checksum. 6 | func New() hash.Hash { 7 | return newDigest(64, iv512) 8 | } 9 | 10 | // NewWithSalt is like New but initializes salt with the given 32-byte slice. 11 | func NewWithSalt(salt []byte) hash.Hash { 12 | d := newDigest(64, iv512) 13 | d.setSalt(salt) 14 | return d 15 | } 16 | 17 | // Sum returns the BLAKE-512 checksum of the data. 18 | func Sum(data []byte) (out [Size]byte) { 19 | d := New() 20 | d.Write(data) 21 | sum := d.Sum(nil) 22 | 23 | copy(out[:], sum) 24 | return 25 | } 26 | 27 | // ========== 28 | 29 | // New384 returns a new hash.Hash computing the BLAKE-384 checksum. 30 | func New384() hash.Hash { 31 | return newDigest(48, iv384) 32 | } 33 | 34 | // New384WithSalt is like New384 but initializes salt with the given 32-byte slice. 35 | func New384WithSalt(salt []byte) hash.Hash { 36 | d := newDigest(48, iv384) 37 | d.setSalt(salt) 38 | return d 39 | } 40 | 41 | // Sum384 returns the BLAKE-384 checksum of the data. 42 | func Sum384(data []byte) (out [Size384]byte) { 43 | d := New384() 44 | d.Write(data) 45 | sum := d.Sum(nil) 46 | 47 | copy(out[:], sum) 48 | return 49 | } 50 | -------------------------------------------------------------------------------- /blake256/binary_test.go: -------------------------------------------------------------------------------- 1 | package blake256 2 | 3 | import ( 4 | "fmt" 5 | "bytes" 6 | "testing" 7 | "encoding" 8 | ) 9 | 10 | func Test_MarshalBinary(t *testing.T) { 11 | msg := []byte("The quick brown fox jumps over the lazy dog") 12 | check := "7576698ee9cad30173080678e5965916adbb11cb5245d386bf1ffda1cb26c9d7" 13 | 14 | h := New() 15 | h.Write(msg) 16 | dst := h.Sum(nil) 17 | if len(dst) == 0 { 18 | t.Error("Hash make error") 19 | } 20 | 21 | bs, _ := h.(encoding.BinaryMarshaler).MarshalBinary() 22 | 23 | h2 := New() 24 | err := h2.(encoding.BinaryUnmarshaler).UnmarshalBinary(bs) 25 | if err != nil { 26 | t.Fatal(err) 27 | } 28 | 29 | newdst := h2.Sum(nil) 30 | if len(newdst) == 0 { 31 | t.Error("newHash make error") 32 | } 33 | 34 | if !bytes.Equal(newdst, dst) { 35 | t.Errorf("Hash MarshalBinary error, got %x, want %x", newdst, dst) 36 | } 37 | 38 | // =========== 39 | 40 | sum1 := fmt.Sprintf("%x", dst) 41 | if sum1 != check { 42 | t.Errorf("Sum error, got %s, want %s", sum1, check) 43 | } 44 | 45 | sum2 := fmt.Sprintf("%x", newdst) 46 | if sum2 != check { 47 | t.Errorf("ReSum error, got %s, want %s", sum2, check) 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /hash/sha_test.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | var sha1Tests = []struct { 9 | input string 10 | output string 11 | }{ 12 | {"sdfgsdgfsdfg123132", "70bad42c28d2cf0e4d3dc5f8f8c628616cee5dc1"}, 13 | {"dfg.;kp[jewijr0-36lsd", "715ea0f7b5d09f660d21d765b4bb52e377f699c2"}, 14 | {"123123", "601f1889667efaebb33b8c12572835da3f027f78"}, 15 | } 16 | 17 | func Test_SHA1(t *testing.T) { 18 | assert := assertT(t) 19 | assertError := assertErrorT(t) 20 | 21 | for index, test := range sha1Tests { 22 | e := FromString(test.input).SHA1() 23 | 24 | t.Run(fmt.Sprintf("SHA1_test_%d", index), func(t *testing.T) { 25 | assertError(e.Error, "SHA1") 26 | assert(test.output, e.ToHexString(), "SHA1") 27 | }) 28 | } 29 | } 30 | 31 | func Test_NewSHA1(t *testing.T) { 32 | assert := assertT(t) 33 | assertError := assertErrorT(t) 34 | 35 | for index, test := range sha1Tests { 36 | e := FromString("").NewSHA1().Write([]byte(test.input)).Sum(nil) 37 | 38 | t.Run(fmt.Sprintf("NewSHA1_test_%d", index), func(t *testing.T) { 39 | assertError(e.Error, "NewSHA1") 40 | assert(test.output, e.ToHexString(), "NewSHA1") 41 | }) 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /sm3/binary_test.go: -------------------------------------------------------------------------------- 1 | package sm3 2 | 3 | import ( 4 | "fmt" 5 | "bytes" 6 | "testing" 7 | ) 8 | 9 | func Test_MarshalBinary(t *testing.T) { 10 | msg := []byte("abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd") 11 | check := "90d52a2e85631a8d6035262626941fa11b85ce570cec1e3e991e2dd7ed258148" 12 | 13 | h := newDigest() 14 | h.Write(msg) 15 | dst := h.Sum(nil) 16 | if len(dst) == 0 { 17 | t.Error("Hash make error") 18 | } 19 | 20 | bs, _ := h.MarshalBinary() 21 | 22 | h2 := newDigest() 23 | err := h2.UnmarshalBinary(bs) 24 | if err != nil { 25 | t.Fatal(err) 26 | } 27 | 28 | newdst := h2.Sum(nil) 29 | if len(newdst) == 0 { 30 | t.Error("newHash make error") 31 | } 32 | 33 | if !bytes.Equal(newdst, dst) { 34 | t.Errorf("Hash MarshalBinary error, got %x, want %x", newdst, dst) 35 | } 36 | 37 | // =========== 38 | 39 | sum1 := fmt.Sprintf("%x", dst) 40 | if sum1 != check { 41 | t.Errorf("Sum error, got %s, want %s", sum1, check) 42 | } 43 | 44 | sum2 := fmt.Sprintf("%x", newdst) 45 | if sum2 != check { 46 | t.Errorf("ReSum error, got %s, want %s", sum2, check) 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /skein/skein1024/skein1024_amd64.go: -------------------------------------------------------------------------------- 1 | // +build amd64 2 | 3 | package skein1024 4 | 5 | import "unsafe" 6 | 7 | func bytesToBlock(block *[16]uint64, src []byte) { 8 | srcPtr := (*[16]uint64)(unsafe.Pointer(&src[0])) 9 | 10 | block[0] = srcPtr[0] 11 | block[1] = srcPtr[1] 12 | block[2] = srcPtr[2] 13 | block[3] = srcPtr[3] 14 | block[4] = srcPtr[4] 15 | block[5] = srcPtr[5] 16 | block[6] = srcPtr[6] 17 | block[7] = srcPtr[7] 18 | block[8] = srcPtr[8] 19 | block[9] = srcPtr[9] 20 | block[10] = srcPtr[10] 21 | block[11] = srcPtr[11] 22 | block[12] = srcPtr[12] 23 | block[13] = srcPtr[13] 24 | block[14] = srcPtr[14] 25 | block[15] = srcPtr[15] 26 | } 27 | 28 | func blockToBytes(dst []byte, block *[16]uint64) { 29 | dstPtr := (*[16]uint64)(unsafe.Pointer(&dst[0])) 30 | 31 | dstPtr[0] = block[0] 32 | dstPtr[1] = block[1] 33 | dstPtr[2] = block[2] 34 | dstPtr[3] = block[3] 35 | dstPtr[4] = block[4] 36 | dstPtr[5] = block[5] 37 | dstPtr[6] = block[6] 38 | dstPtr[7] = block[7] 39 | dstPtr[8] = block[8] 40 | dstPtr[9] = block[9] 41 | dstPtr[10] = block[10] 42 | dstPtr[11] = block[11] 43 | dstPtr[12] = block[12] 44 | dstPtr[13] = block[13] 45 | dstPtr[14] = block[14] 46 | dstPtr[15] = block[15] 47 | } 48 | -------------------------------------------------------------------------------- /blake512/binary_test.go: -------------------------------------------------------------------------------- 1 | package blake512 2 | 3 | import ( 4 | "fmt" 5 | "bytes" 6 | "testing" 7 | "encoding" 8 | ) 9 | 10 | func Test_MarshalBinary(t *testing.T) { 11 | msg := fromHex("eed7422227613b6f53c9") 12 | check := "c5633a1b9e45cef38647603cbd9710e1aca4f2fb84f8d56a0d729fd6d480ef05f8a46f1dc0e771ec114aea2f9ad534b70bf03046118a5f2fbdd371442d9d8895" 13 | 14 | h := New() 15 | h.Write(msg) 16 | dst := h.Sum(nil) 17 | if len(dst) == 0 { 18 | t.Error("Hash make error") 19 | } 20 | 21 | bs, _ := h.(encoding.BinaryMarshaler).MarshalBinary() 22 | 23 | h2 := New() 24 | err := h2.(encoding.BinaryUnmarshaler).UnmarshalBinary(bs) 25 | if err != nil { 26 | t.Fatal(err) 27 | } 28 | 29 | newdst := h2.Sum(nil) 30 | if len(newdst) == 0 { 31 | t.Error("newHash make error") 32 | } 33 | 34 | if !bytes.Equal(newdst, dst) { 35 | t.Errorf("Hash MarshalBinary error, got %x, want %x", newdst, dst) 36 | } 37 | 38 | // =========== 39 | 40 | sum1 := fmt.Sprintf("%x", dst) 41 | if sum1 != check { 42 | t.Errorf("Sum error, got %s, want %s", sum1, check) 43 | } 44 | 45 | sum2 := fmt.Sprintf("%x", newdst) 46 | if sum2 != check { 47 | t.Errorf("ReSum error, got %s, want %s", sum2, check) 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /gost/gost34112012256/esptree.go: -------------------------------------------------------------------------------- 1 | package gost34112012256 2 | 3 | import ( 4 | "crypto/subtle" 5 | ) 6 | 7 | type ESPTree struct { 8 | keyRoot []byte 9 | isPrev [5]byte 10 | key []byte 11 | } 12 | 13 | func NewESPTree(keyRoot []byte) *ESPTree { 14 | key := make([]byte, len(keyRoot)) 15 | copy(key, keyRoot) 16 | 17 | t := &ESPTree{ 18 | keyRoot: key, 19 | key: make([]byte, Size), 20 | } 21 | 22 | t.isPrev[0]++ 23 | t.DeriveCached([]byte{0x00, 0x00, 0x00, 0x00, 0x00}) 24 | 25 | return t 26 | } 27 | 28 | func (t *ESPTree) DeriveCached(is []byte) ([]byte, bool) { 29 | if len(is) != 1+2+2 { 30 | panic("invalid i1+i2+i3 input") 31 | } 32 | 33 | if subtle.ConstantTimeCompare(t.isPrev[:], is) == 1 { 34 | return t.key, true 35 | } 36 | 37 | pass1 := KDF(New, t.keyRoot, []byte("level1"), append([]byte{0}, is[0])) 38 | pass2 := KDF(New, pass1, []byte("level2"), is[1:3]) 39 | pass3 := KDF(New, pass2, []byte("level3"), is[3:5]) 40 | 41 | copy(t.isPrev[:], is) 42 | 43 | t.key = pass3 44 | 45 | return t.key, false 46 | } 47 | 48 | func (t *ESPTree) Derive(is []byte) []byte { 49 | key, _ := t.DeriveCached(is) 50 | 51 | keyDerived := make([]byte, Size) 52 | copy(keyDerived, key) 53 | 54 | return keyDerived 55 | } 56 | -------------------------------------------------------------------------------- /hash/echo.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "github.com/deatil/go-hash/echo" 5 | ) 6 | 7 | // Echo224 哈希值 8 | func (this Hash) Echo224() Hash { 9 | sum := echo.Sum224(this.data) 10 | this.data = sum[:] 11 | 12 | return this 13 | } 14 | 15 | // NewEcho224 16 | func (this Hash) NewEcho224() Hash { 17 | this.hash = echo.New224() 18 | 19 | return this 20 | } 21 | 22 | // =========== 23 | 24 | // Echo256 哈希值 25 | func (this Hash) Echo256() Hash { 26 | sum := echo.Sum256(this.data) 27 | this.data = sum[:] 28 | 29 | return this 30 | } 31 | 32 | // NewEcho256 33 | func (this Hash) NewEcho256() Hash { 34 | this.hash = echo.New256() 35 | 36 | return this 37 | } 38 | 39 | // =========== 40 | 41 | // Echo384 哈希值 42 | func (this Hash) Echo384() Hash { 43 | sum := echo.Sum384(this.data) 44 | this.data = sum[:] 45 | 46 | return this 47 | } 48 | 49 | // NewEcho384 50 | func (this Hash) NewEcho384() Hash { 51 | this.hash = echo.New384() 52 | 53 | return this 54 | } 55 | 56 | // =========== 57 | 58 | // Echo512 哈希值 59 | func (this Hash) Echo512() Hash { 60 | sum := echo.Sum512(this.data) 61 | this.data = sum[:] 62 | 63 | return this 64 | } 65 | 66 | // NewEcho512 67 | func (this Hash) NewEcho512() Hash { 68 | this.hash = echo.New512() 69 | 70 | return this 71 | } 72 | -------------------------------------------------------------------------------- /md2/sbox.go: -------------------------------------------------------------------------------- 1 | package md2 2 | 3 | var sbox = []uint8{ 4 | 41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6, 5 | 19, 98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188, 6 | 76, 130, 202, 30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24, 7 | 138, 23, 229, 18, 190, 78, 196, 214, 218, 158, 222, 73, 160, 251, 8 | 245, 142, 187, 47, 238, 122, 169, 104, 121, 145, 21, 178, 7, 63, 9 | 148, 194, 16, 137, 11, 34, 95, 33, 128, 127, 93, 154, 90, 144, 50, 10 | 39, 53, 62, 204, 231, 191, 247, 151, 3, 255, 25, 48, 179, 72, 165, 11 | 181, 209, 215, 94, 146, 42, 172, 86, 170, 198, 79, 184, 56, 210, 12 | 150, 164, 125, 182, 118, 252, 107, 226, 156, 116, 4, 241, 69, 157, 13 | 112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2, 27, 14 | 96, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15, 15 | 85, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197, 16 | 234, 38, 44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65, 17 | 129, 77, 82, 106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123, 18 | 8, 12, 189, 177, 74, 120, 136, 149, 139, 227, 99, 232, 109, 233, 19 | 203, 213, 254, 59, 0, 29, 57, 242, 239, 183, 14, 102, 88, 208, 228, 20 | 166, 119, 114, 248, 235, 117, 75, 10, 49, 68, 80, 180, 143, 237, 21 | 31, 26, 219, 153, 141, 51, 159, 17, 131, 20, 22 | } 23 | -------------------------------------------------------------------------------- /hash/siphash_test.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "bytes" 5 | "testing" 6 | ) 7 | 8 | func Test_Siphash64(t *testing.T) { 9 | var key [16]byte 10 | var in []byte 11 | var i int 12 | 13 | expected := []byte{ 0xdb, 0x9b, 0xc2, 0x57, 0x7f, 0xcc, 0x2a, 0x3f, } 14 | 15 | for i = 0; i < 16; i++ { 16 | key[i] = byte(i) 17 | } 18 | 19 | inlen := 16 20 | in = make([]byte, inlen) 21 | for i = 0; i < inlen; i++ { 22 | in[i] = byte(i) 23 | } 24 | 25 | res := FromBytes(in[:]).Siphash64(key[:]).ToBytes() 26 | 27 | if !bytes.Equal(expected, res) { 28 | t.Errorf("Check Hash error, got %x, want %x", res, expected) 29 | } 30 | } 31 | 32 | func Test_Siphash128(t *testing.T) { 33 | var key [16]byte 34 | var in []byte 35 | var i int 36 | 37 | expected := []byte{ 0x9e, 0x25, 0xfc, 0x83, 0x3f, 0x22, 0x90, 0x73, 0x3e, 0x93, 0x44, 0xa5, 0xe8, 0x38, 0x39, 0xeb, } 38 | 39 | for i = 0; i < 16; i++ { 40 | key[i] = byte(i) 41 | } 42 | 43 | inlen := 20 44 | in = make([]byte, inlen) 45 | for i = 0; i < inlen; i++ { 46 | in[i] = byte(i) 47 | } 48 | 49 | res := FromBytes(in[:]).Siphash128(key[:]).ToBytes() 50 | 51 | if !bytes.Equal(expected, res) { 52 | t.Errorf("Check Hash error, got %x, want %x", res, expected) 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /tool/deepcopy/deepcopy_test.go: -------------------------------------------------------------------------------- 1 | package deepcopy 2 | 3 | import ( 4 | "testing" 5 | "reflect" 6 | ) 7 | 8 | func AssertEqualT(t *testing.T) func(any, any, string) { 9 | return func(actual any, expected any, msg string) { 10 | if !reflect.DeepEqual(actual, expected) { 11 | t.Errorf("Failed %s: actual: %v, expected: %v", msg, actual, expected) 12 | } 13 | } 14 | } 15 | 16 | type testPtr struct { 17 | B string 18 | C []byte 19 | } 20 | 21 | func Test_Check(t *testing.T) { 22 | assertEqual := AssertEqualT(t) 23 | 24 | { 25 | ptra := testPtr{ 26 | B: "bbbbbbbbbbbb", 27 | C: []byte("cccccccccc"), 28 | } 29 | ptrb := Copy(ptra) 30 | 31 | assertEqual(ptrb, ptra, "Test_Check-Ptr") 32 | } 33 | 34 | { 35 | ptra := []byte("cccccccccc") 36 | ptrb := Copy(ptra) 37 | 38 | assertEqual(ptrb, ptra, "Test_Check-Slice") 39 | } 40 | 41 | { 42 | ptra := map[string]any{ 43 | "a": "aaaaaaaa", 44 | "cc": "cccccccccc", 45 | "ddd": 111111, 46 | } 47 | ptrb := Copy(ptra) 48 | 49 | assertEqual(ptrb, ptra, "Test_Check-map") 50 | } 51 | 52 | { 53 | var ptra any = "ttttttttttt" 54 | ptrb := Copy(ptra) 55 | 56 | assertEqual(ptrb, ptra, "Test_Check-map") 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /blake512/utils.go: -------------------------------------------------------------------------------- 1 | package blake512 2 | 3 | import ( 4 | "math/bits" 5 | "encoding/binary" 6 | ) 7 | 8 | func getu32(ptr []byte) uint32 { 9 | return binary.BigEndian.Uint32(ptr) 10 | } 11 | 12 | func putu32(ptr []byte, a uint32) { 13 | binary.BigEndian.PutUint32(ptr, a) 14 | } 15 | 16 | func getu64(ptr []byte) uint64 { 17 | return binary.BigEndian.Uint64(ptr) 18 | } 19 | 20 | func putu64(ptr []byte, a uint64) { 21 | binary.BigEndian.PutUint64(ptr, a) 22 | } 23 | 24 | func bytesToUint32s(b []byte) []uint32 { 25 | size := len(b) / 4 26 | dst := make([]uint32, size) 27 | 28 | for i := 0; i < size; i++ { 29 | j := i * 4 30 | 31 | dst[i] = binary.BigEndian.Uint32(b[j:]) 32 | } 33 | 34 | return dst 35 | } 36 | 37 | func uint32sToBytes(w []uint32) []byte { 38 | size := len(w) * 4 39 | dst := make([]byte, size) 40 | 41 | for i := 0; i < len(w); i++ { 42 | j := i * 4 43 | 44 | binary.BigEndian.PutUint32(dst[j:], w[i]) 45 | } 46 | 47 | return dst 48 | } 49 | 50 | func bytesToUint64s(b []byte) []uint64 { 51 | size := len(b) / 8 52 | dst := make([]uint64, size) 53 | 54 | for i := 0; i < size; i++ { 55 | j := i * 8 56 | 57 | dst[i] = binary.BigEndian.Uint64(b[j:]) 58 | } 59 | 60 | return dst 61 | } 62 | 63 | func circularRight(x uint64, n int) uint64 { 64 | return bits.RotateLeft64(x, 64 - n) 65 | } 66 | -------------------------------------------------------------------------------- /xxhash/xxhash32/utils.go: -------------------------------------------------------------------------------- 1 | package xxhash32 2 | 3 | import ( 4 | "math/bits" 5 | "encoding/binary" 6 | ) 7 | 8 | func getu32(ptr []byte) uint32 { 9 | return binary.LittleEndian.Uint32(ptr) 10 | } 11 | 12 | func putu32(ptr []byte, a uint32) { 13 | binary.LittleEndian.PutUint32(ptr, a) 14 | } 15 | 16 | func putu32be(ptr []byte, a uint32) { 17 | binary.BigEndian.PutUint32(ptr, a) 18 | } 19 | 20 | func bytesToUint32s(b []byte) []uint32 { 21 | size := len(b) / 4 22 | dst := make([]uint32, size) 23 | 24 | for i := 0; i < size; i++ { 25 | j := i * 4 26 | 27 | dst[i] = binary.LittleEndian.Uint32(b[j:]) 28 | } 29 | 30 | return dst 31 | } 32 | 33 | func uint32sToBytes(w []uint32) []byte { 34 | size := len(w) * 4 35 | dst := make([]byte, size) 36 | 37 | for i := 0; i < len(w); i++ { 38 | j := i * 4 39 | 40 | binary.LittleEndian.PutUint32(dst[j:], w[i]) 41 | } 42 | 43 | return dst 44 | } 45 | 46 | func rotl(x, n uint32) uint32 { 47 | return bits.RotateLeft32(x, int(n)) 48 | } 49 | 50 | func round(acc, input uint32) uint32 { 51 | acc += input * prime[1] 52 | acc = rotl(acc, 13) 53 | acc *= prime[0] 54 | 55 | return acc 56 | } 57 | 58 | func avalanche(h32 uint32) uint32 { 59 | h32 ^= h32 >> 15 60 | h32 *= prime[1] 61 | h32 ^= h32 >> 13 62 | h32 *= prime[2] 63 | h32 ^= h32 >> 16 64 | 65 | return h32 66 | } 67 | -------------------------------------------------------------------------------- /hash/crc32.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "hash/crc32" 5 | ) 6 | 7 | // CRC32IEEE 哈希值 8 | func (this Hash) CRC32_IEEE() Hash { 9 | h := crc32.NewIEEE() 10 | h.Write(this.data) 11 | 12 | this.data = h.Sum(nil) 13 | 14 | return this 15 | } 16 | 17 | // NewCRC32_IEEE 18 | func (this Hash) NewCRC32_IEEE() Hash { 19 | this.hash = crc32.NewIEEE() 20 | 21 | return this 22 | } 23 | 24 | // ============================================ 25 | 26 | // CRC32Castagnoli 哈希值 27 | func (this Hash) CRC32_Castagnoli() Hash { 28 | tab := crc32.MakeTable(crc32.Castagnoli) 29 | 30 | h := crc32.New(tab) 31 | h.Write(this.data) 32 | 33 | this.data = h.Sum(nil) 34 | 35 | return this 36 | } 37 | 38 | // NewCRC32_Castagnoli 39 | func (this Hash) NewCRC32_Castagnoli() Hash { 40 | tab := crc32.MakeTable(crc32.Castagnoli) 41 | 42 | this.hash = crc32.New(tab) 43 | 44 | return this 45 | } 46 | 47 | // ============================================ 48 | 49 | // CRC32Koopman 哈希值 50 | func (this Hash) CRC32_Koopman() Hash { 51 | tab := crc32.MakeTable(crc32.Koopman) 52 | 53 | h := crc32.New(tab) 54 | h.Write(this.data) 55 | 56 | this.data = h.Sum(nil) 57 | 58 | return this 59 | } 60 | 61 | // NewCRC32_Koopman 62 | func (this Hash) NewCRC32_Koopman() Hash { 63 | tab := crc32.MakeTable(crc32.Koopman) 64 | 65 | this.hash = crc32.New(tab) 66 | 67 | return this 68 | } 69 | -------------------------------------------------------------------------------- /murmur3/digest.go: -------------------------------------------------------------------------------- 1 | package murmur3 2 | 3 | type bmixer interface { 4 | bmix(p []byte) (tail []byte) 5 | Size() (n int) 6 | reset() 7 | } 8 | 9 | type digest struct { 10 | clen int // Digested input cumulative length. 11 | tail []byte // 0 to Size()-1 bytes view of `buf'. 12 | buf [16]byte // Expected (but not required) to be Size() large. 13 | seed uint32 // Seed for initializing the hash. 14 | bmixer 15 | } 16 | 17 | func (d *digest) BlockSize() int { 18 | return 1 19 | } 20 | 21 | func (d *digest) Reset() { 22 | d.clen = 0 23 | d.tail = nil 24 | d.bmixer.reset() 25 | } 26 | 27 | func (d *digest) Write(p []byte) (n int, err error) { 28 | n = len(p) 29 | d.clen += n 30 | 31 | if len(d.tail) > 0 { 32 | // Stick back pending bytes. 33 | nfree := d.Size() - len(d.tail) // nfree ∈ [1, d.Size()-1]. 34 | if nfree < len(p) { 35 | // One full block can be formed. 36 | block := append(d.tail, p[:nfree]...) 37 | p = p[nfree:] 38 | _ = d.bmix(block) // No tail. 39 | } else { 40 | // Tail's buf is large enough to prevent reallocs. 41 | p = append(d.tail, p...) 42 | } 43 | } 44 | 45 | d.tail = d.bmix(p) 46 | 47 | // Keep own copy of the 0 to Size()-1 pending bytes. 48 | nn := copy(d.buf[:], d.tail) 49 | d.tail = d.buf[:nn] 50 | 51 | return n, nil 52 | } 53 | -------------------------------------------------------------------------------- /simd/utils.go: -------------------------------------------------------------------------------- 1 | package simd 2 | 3 | import ( 4 | "math/bits" 5 | "encoding/binary" 6 | ) 7 | 8 | func getu32(ptr []byte) uint32 { 9 | return binary.LittleEndian.Uint32(ptr) 10 | } 11 | 12 | func putu32(ptr []byte, a uint32) { 13 | binary.LittleEndian.PutUint32(ptr, a) 14 | } 15 | 16 | func getu64(ptr []byte) uint64 { 17 | return binary.LittleEndian.Uint64(ptr) 18 | } 19 | 20 | func putu64(ptr []byte, a uint64) { 21 | binary.LittleEndian.PutUint64(ptr, a) 22 | } 23 | 24 | func bytesToUint32s(b []byte) []uint32 { 25 | size := len(b) / 4 26 | dst := make([]uint32, size) 27 | 28 | for i := 0; i < size; i++ { 29 | j := i * 4 30 | 31 | dst[i] = binary.LittleEndian.Uint32(b[j:]) 32 | } 33 | 34 | return dst 35 | } 36 | 37 | func uint32sToBytes(w []uint32) []byte { 38 | size := len(w) * 4 39 | dst := make([]byte, size) 40 | 41 | for i := 0; i < len(w); i++ { 42 | j := i * 4 43 | 44 | binary.LittleEndian.PutUint32(dst[j:], w[i]) 45 | } 46 | 47 | return dst 48 | } 49 | 50 | func circularLeft(x uint32, n int) uint32 { 51 | return bits.RotateLeft32(x, n) 52 | } 53 | 54 | func REDS1(x int32) int32 { 55 | return (x & 0xFF) - (x >> 8) 56 | } 57 | 58 | func REDS2(x int32) int32 { 59 | return (x & 0xFFFF) + (x >> 16) 60 | } 61 | 62 | func INNER(l, h, mm int32) uint32 { 63 | return (uint32(l * mm) & 0xFFFF) + (uint32(h * mm) << 16) 64 | } 65 | -------------------------------------------------------------------------------- /sm3/utils.go: -------------------------------------------------------------------------------- 1 | package sm3 2 | 3 | import ( 4 | "math/bits" 5 | "encoding/binary" 6 | ) 7 | 8 | func GETU32(ptr []byte) uint32 { 9 | return binary.BigEndian.Uint32(ptr) 10 | } 11 | 12 | func PUTU32(ptr []byte, a uint32) { 13 | binary.BigEndian.PutUint32(ptr, a) 14 | } 15 | 16 | func ROTL(x uint32, n uint32) uint32 { 17 | return bits.RotateLeft32(x, int(n)) 18 | } 19 | 20 | func P0(x uint32) uint32 { 21 | return x ^ ROTL(x, 9) ^ ROTL(x, 17) 22 | } 23 | 24 | func P1(x uint32) uint32 { 25 | return x ^ ROTL(x, 15) ^ ROTL(x, 23) 26 | } 27 | 28 | func FF00(x, y, z uint32) uint32 { 29 | return x ^ y ^ z 30 | } 31 | 32 | func FF16(x, y, z uint32) uint32 { 33 | return (x & y) | (x & z) | (y & z) 34 | } 35 | 36 | func GG00(x, y, z uint32) uint32 { 37 | return x ^ y ^ z 38 | } 39 | 40 | func GG16(x, y, z uint32) uint32 { 41 | return ((y ^ z) & x) ^ z 42 | } 43 | 44 | func GenT() []uint32 { 45 | init1 := 0x79CC4519 46 | init2 := 0x7A879D8A 47 | 48 | var T = make([]uint32, 0) 49 | for j := 0; j < 16; j++ { 50 | Tj := (init1 << uint32(j)) | (init1 >> (32 - uint32(j))) 51 | 52 | T = append(T, uint32(Tj)) 53 | } 54 | 55 | for j := 16; j < 64; j++ { 56 | n := j % 32 57 | Tj := (init2 << uint32(n)) | (init2 >> (32 - uint32(n))) 58 | 59 | T = append(T, uint32(Tj)) 60 | } 61 | 62 | // fmt.Printf("0x%08X, ", Tj) 63 | 64 | return T 65 | } 66 | -------------------------------------------------------------------------------- /rabin/rabin.go: -------------------------------------------------------------------------------- 1 | // Package rabin implements Rabin hashing (fingerprinting). 2 | package rabin 3 | 4 | import ( 5 | "hash" 6 | ) 7 | 8 | // 9 | // A given Rabin hash function is defined by a polynomial over GF(2): 10 | // 11 | // p(x) = ... + p₂x² + p₁x + p₀ where pₙ ∈ GF(2) 12 | // 13 | // The message to be hashed is likewise interpreted as a polynomial 14 | // over GF(2), where the coefficients are the bits of the message in 15 | // left-to-right most-significant-bit-first order. Given a message 16 | // polynomial m(x) and a hashing polynomial p(x), the Rabin hash is 17 | // simply the coefficients of m(x) mod p(x). 18 | // 19 | // Rabin hashing has the unusual property that it can efficiently 20 | // compute a "rolling hash" of a stream of data, where the hash value 21 | // reflects only the most recent w bytes of the stream, for some 22 | // window size w. This property makes it ideal for "content-defined 23 | // chunking", which sub-divides sequential data on boundaries that are 24 | // robust to insertions and deletions. 25 | // 26 | // The details of Rabin hashing are described in Rabin, Michael 27 | // (1981). "Fingerprinting by Random Polynomials." Center for Research 28 | // in Computing Technology, Harvard University. Tech Report 29 | // TR-CSE-03-01. 30 | 31 | // New returns a new Rabin hash using the polynomial and window size 32 | // represented by table. 33 | func New(table *Table) hash.Hash64 { 34 | return newDigest(table) 35 | } 36 | -------------------------------------------------------------------------------- /ripemd/ripemd.go: -------------------------------------------------------------------------------- 1 | package ripemd 2 | 3 | import ( 4 | "hash" 5 | ) 6 | 7 | func New128() hash.Hash { 8 | return newDigest128() 9 | } 10 | 11 | // Sum128 returns checksum of the data. 12 | func Sum128(data []byte) (sum [Size128]byte) { 13 | h := New128() 14 | h.Write(data) 15 | hash := h.Sum(nil) 16 | 17 | copy(sum[:], hash) 18 | return 19 | } 20 | 21 | // ============ 22 | 23 | // New160 returns a new hash.Hash computing the checksum. 24 | func New160() hash.Hash { 25 | h := newDigest160() 26 | return h 27 | } 28 | 29 | // Sum160 returns checksum of the data. 30 | func Sum160(data []byte) (sum [Size160]byte) { 31 | h := New160() 32 | h.Write(data) 33 | hash := h.Sum(nil) 34 | 35 | copy(sum[:], hash) 36 | return 37 | } 38 | 39 | // ============ 40 | 41 | func New256() hash.Hash { 42 | h := newDigest256() 43 | return h 44 | } 45 | 46 | // Sum256 returns checksum of the data. 47 | func Sum256(data []byte) (sum [Size256]byte) { 48 | h := New256() 49 | h.Write(data) 50 | hash := h.Sum(nil) 51 | 52 | copy(sum[:], hash) 53 | return 54 | } 55 | 56 | // ============ 57 | 58 | func New320() hash.Hash { 59 | h := newDigest320() 60 | return h 61 | } 62 | 63 | // Sum320 returns checksum of the data. 64 | func Sum320(data []byte) (sum [Size320]byte) { 65 | h := New320() 66 | h.Write(data) 67 | hash := h.Sum(nil) 68 | 69 | copy(sum[:], hash) 70 | return 71 | } 72 | -------------------------------------------------------------------------------- /hash/lsh512.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "github.com/deatil/go-hash/lsh512" 5 | ) 6 | 7 | // LSH512 8 | func (this Hash) LSH512() Hash { 9 | h := lsh512.New() 10 | h.Write(this.data) 11 | 12 | this.data = h.Sum(nil) 13 | 14 | return this 15 | } 16 | 17 | // NewLSH512 18 | func (this Hash) NewLSH512() Hash { 19 | this.hash = lsh512.New() 20 | 21 | return this 22 | } 23 | 24 | // =========== 25 | 26 | // LSH512_384 27 | func (this Hash) LSH512_384() Hash { 28 | h := lsh512.New384() 29 | h.Write(this.data) 30 | 31 | this.data = h.Sum(nil) 32 | 33 | return this 34 | } 35 | 36 | // NewLSH512_384 37 | func (this Hash) NewLSH512_384() Hash { 38 | this.hash = lsh512.New384() 39 | 40 | return this 41 | } 42 | 43 | // =========== 44 | 45 | // LSH512_256 46 | func (this Hash) LSH512_256() Hash { 47 | h := lsh512.New256() 48 | h.Write(this.data) 49 | 50 | this.data = h.Sum(nil) 51 | 52 | return this 53 | } 54 | 55 | // NewLSH512_256 56 | func (this Hash) NewLSH512_256() Hash { 57 | this.hash = lsh512.New256() 58 | 59 | return this 60 | } 61 | 62 | // =========== 63 | 64 | // LSH512_224 65 | func (this Hash) LSH512_224() Hash { 66 | h := lsh512.New224() 67 | h.Write(this.data) 68 | 69 | this.data = h.Sum(nil) 70 | 71 | return this 72 | } 73 | 74 | // NewLSH512_224 75 | func (this Hash) NewLSH512_224() Hash { 76 | this.hash = lsh512.New224() 77 | 78 | return this 79 | } 80 | -------------------------------------------------------------------------------- /gost/gost34112012256/hash_test.go: -------------------------------------------------------------------------------- 1 | package gost34112012256 2 | 3 | import ( 4 | "fmt" 5 | "hash" 6 | "testing" 7 | "encoding/binary" 8 | ) 9 | 10 | func TestHashInterface(t *testing.T) { 11 | h := New() 12 | var _ hash.Hash = h 13 | } 14 | 15 | func TestHashed(t *testing.T) { 16 | h := New() 17 | m := make([]byte, BlockSize) 18 | for i := 0; i < BlockSize; i++ { 19 | m[i] = byte(i) 20 | } 21 | 22 | h.Write(m) 23 | hashed := h.Sum(nil) 24 | 25 | if len(hashed) == 0 { 26 | t.Error("Hash error") 27 | } 28 | } 29 | 30 | func Test_ESPTree(t *testing.T) { 31 | data := NewESPTree([]byte("rgtf5yds")).Derive([]byte("olkpj")) 32 | 33 | if len(data) == 0 { 34 | t.Error("ESPTree data error") 35 | } 36 | } 37 | 38 | func Test_TLSTree(t *testing.T) { 39 | num := binary.BigEndian.Uint64([]byte{0xFE, 0xFF, 0xFF, 0xC0, 0x00, 0x00, 0x00, 0x00}) 40 | 41 | data := NewTLSTree(TLSKuznyechikCTROMAC, []byte("rgtf5yds")).Derive(num) 42 | 43 | if len(data) == 0 { 44 | t.Error("TLSTree data error") 45 | } 46 | } 47 | 48 | func Test_Check(t *testing.T) { 49 | in := []byte("nonce-asdfg123123123") 50 | check := "f24a63bbb863ba538ad956ababb0c4a651136a4d81c878a818bad28c9094d8e1" 51 | 52 | h := New() 53 | h.Write(in) 54 | 55 | out := h.Sum(nil) 56 | 57 | if fmt.Sprintf("%x", out) != check { 58 | t.Errorf("Check error. got %x, want %s", out, check) 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /hash/blake512_test.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | func Test_Blake512(t *testing.T) { 8 | eq := assertT(t) 9 | err := assertErrorT(t) 10 | 11 | msg := fromHex("c6f50bb74e29") 12 | md := "b6e8a7380df1f007d7c271e7255bbca7714f25029ac1fd6fe92ef74cbcd9e99c112f8ae1a45ccb566ce19d9678a122c612beff5f8eeeee3f3f402fd2781182d4" 13 | 14 | t.Run("Sum", func(t *testing.T) { 15 | e := FromBytes(msg).Blake512() 16 | 17 | err(e.Error, "Sum") 18 | eq(e.ToHexString(), md, "Sum") 19 | }) 20 | 21 | t.Run("New", func(t *testing.T) { 22 | e := Hashing(). 23 | NewBlake512(). 24 | Write(msg). 25 | Sum(nil) 26 | 27 | err(e.Error, "New") 28 | eq(e.ToHexString(), md, "New") 29 | }) 30 | } 31 | 32 | func Test_Blake384(t *testing.T) { 33 | eq := assertT(t) 34 | err := assertErrorT(t) 35 | 36 | msg := fromHex("c6f50bb74e29") 37 | md := "5ddb50068ca430bffae7e5a8bbcb2c59171743cce027c0ea937fa2b511848192af2aca98ead30b0850b4d2d1542decdb" 38 | 39 | t.Run("Sum", func(t *testing.T) { 40 | e := FromBytes(msg).Blake384() 41 | 42 | err(e.Error, "Sum") 43 | eq(e.ToHexString(), md, "Sum") 44 | }) 45 | 46 | t.Run("New", func(t *testing.T) { 47 | e := Hashing(). 48 | NewBlake384(). 49 | Write(msg). 50 | Sum(nil) 51 | 52 | err(e.Error, "New") 53 | eq(e.ToHexString(), md, "New") 54 | }) 55 | } 56 | -------------------------------------------------------------------------------- /sm3/rand_df.go: -------------------------------------------------------------------------------- 1 | package sm3 2 | 3 | import ( 4 | "hash" 5 | ) 6 | 7 | // The size of an SM3-DF checksum in bytes. 8 | const SizeDF = 55 9 | 10 | // The blocksize of SM3-DF in bytes. 11 | const BlockSizeDF = 64 12 | 13 | type DF struct { 14 | sm3 [2]hash.Hash 15 | } 16 | 17 | func NewDF() *DF { 18 | df := new(DF) 19 | df.Reset() 20 | 21 | return df 22 | } 23 | 24 | func (this *DF) Reset() { 25 | var counter = [4]byte{0, 0, 0, 1} 26 | var seedlen = [4]byte{0, 0, 440/256, 440%256} 27 | 28 | this.sm3[0] = New() 29 | this.sm3[0].Write(counter[:]) 30 | this.sm3[0].Write(seedlen[:]) 31 | 32 | counter[3] = 2 33 | 34 | this.sm3[1] = New() 35 | this.sm3[1].Write(counter[:]) 36 | this.sm3[1].Write(seedlen[:]) 37 | } 38 | 39 | func (this *DF) Size() int { 40 | return SizeDF 41 | } 42 | 43 | func (this *DF) BlockSize() int { 44 | return BlockSizeDF 45 | } 46 | 47 | func (this *DF) Write(data []byte) { 48 | if len(data) > 0 { 49 | this.sm3[0].Write(data) 50 | this.sm3[1].Write(data) 51 | } 52 | } 53 | 54 | func (this *DF) Sum(in []byte) []byte { 55 | // Make a copy of d so that caller can keep writing and summing. 56 | d := *this 57 | sum := d.checkSum() 58 | return append(in, sum[:]...) 59 | } 60 | 61 | func (this *DF) checkSum() (out [SizeDF]byte) { 62 | o := this.sm3[0].Sum(nil) 63 | buf := this.sm3[1].Sum(nil) 64 | 65 | copy(out[:], o) 66 | copy(out[:SizeDF - 32], buf) 67 | 68 | return 69 | } 70 | -------------------------------------------------------------------------------- /hash/sha3.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "golang.org/x/crypto/sha3" 5 | ) 6 | 7 | // SHA3_224 哈希值 8 | func (this Hash) SHA3_224() Hash { 9 | h := sha3.New224() 10 | h.Write(this.data) 11 | 12 | this.data = h.Sum(nil) 13 | 14 | return this 15 | } 16 | 17 | // NewSHA3_224 18 | func (this Hash) NewSHA3_224() Hash { 19 | this.hash = sha3.New224() 20 | 21 | return this 22 | } 23 | 24 | // ============================================ 25 | 26 | // SHA3_256 哈希值 27 | func (this Hash) SHA3_256() Hash { 28 | h := sha3.New256() 29 | h.Write(this.data) 30 | 31 | this.data = h.Sum(nil) 32 | 33 | return this 34 | } 35 | 36 | // NewSHA3_256 37 | func (this Hash) NewSHA3_256() Hash { 38 | this.hash = sha3.New256() 39 | 40 | return this 41 | } 42 | 43 | // ============================================ 44 | 45 | // SHA3_384 哈希值 46 | func (this Hash) SHA3_384() Hash { 47 | h := sha3.New384() 48 | h.Write(this.data) 49 | 50 | this.data = h.Sum(nil) 51 | 52 | return this 53 | } 54 | 55 | // NewSHA3_384 56 | func (this Hash) NewSHA3_384() Hash { 57 | this.hash = sha3.New384() 58 | 59 | return this 60 | } 61 | 62 | // ============================================ 63 | 64 | // SHA3_512 哈希值 65 | func (this Hash) SHA3_512() Hash { 66 | h := sha3.New512() 67 | h.Write(this.data) 68 | 69 | this.data = h.Sum(nil) 70 | 71 | return this 72 | } 73 | 74 | // NewSHA3_512 75 | func (this Hash) NewSHA3_512() Hash { 76 | this.hash = sha3.New512() 77 | 78 | return this 79 | } 80 | -------------------------------------------------------------------------------- /hash/siphash.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "github.com/deatil/go-hash/siphash" 5 | ) 6 | 7 | // Siphash64 8 | func (this Hash) Siphash64(k []byte) Hash { 9 | h := siphash.New64(k) 10 | h.Write(this.data) 11 | 12 | this.data = h.Sum(nil) 13 | 14 | return this 15 | } 16 | 17 | // NewSiphash64 18 | func (this Hash) NewSiphash64(k []byte) Hash { 19 | this.hash = siphash.New64(k) 20 | 21 | return this 22 | } 23 | 24 | // ============== 25 | 26 | // Siphash128 27 | func (this Hash) Siphash128(k []byte) Hash { 28 | h := siphash.New128(k) 29 | h.Write(this.data) 30 | 31 | this.data = h.Sum(nil) 32 | 33 | return this 34 | } 35 | 36 | // NewSiphash128 37 | func (this Hash) NewSiphash128(k []byte) Hash { 38 | this.hash = siphash.New128(k) 39 | 40 | return this 41 | } 42 | 43 | // ============== 44 | 45 | // SiphashWithCDroundsAndHashSize 46 | func (this Hash) SiphashWithCDroundsAndHashSize(k []byte, crounds, drounds int32, hashSize int) Hash { 47 | h, err := siphash.NewWithCDroundsAndHashSize(k, crounds, drounds, hashSize) 48 | if err != nil { 49 | this.Error = err 50 | return this 51 | } 52 | 53 | h.Write(this.data) 54 | this.data = h.Sum(nil) 55 | 56 | return this 57 | } 58 | 59 | // NewSiphashWithCDroundsAndHashSize 60 | func (this Hash) NewSiphashWithCDroundsAndHashSize(k []byte, crounds, drounds int32, hashSize int) Hash { 61 | this.hash, this.Error = siphash.NewWithCDroundsAndHashSize(k, crounds, drounds, hashSize) 62 | 63 | return this 64 | } 65 | -------------------------------------------------------------------------------- /cmac/aes/cmac.go: -------------------------------------------------------------------------------- 1 | // Package aes implements the CMAC MAC with the AES. 2 | // AES-CMAC is specified in RFC 4493 and RFC 4494. 3 | package aes 4 | 5 | import ( 6 | "hash" 7 | "crypto/aes" 8 | 9 | "github.com/deatil/go-hash/cmac" 10 | ) 11 | 12 | // Sum computes the AES-CMAC checksum with the given tagsize of msg using the cipher.Block. 13 | func Sum(msg, key []byte, tagsize int) ([]byte, error) { 14 | c, err := aes.NewCipher(key) 15 | if err != nil { 16 | return nil, err 17 | } 18 | return cmac.Sum(msg, c, tagsize) 19 | } 20 | 21 | // Verify computes the AES-CMAC checksum with the given tagsize of msg and compares 22 | // it with the given mac. This functions returns true if and only if the given mac 23 | // is equal to the computed one. 24 | func Verify(mac, msg, key []byte, tagsize int) bool { 25 | c, err := aes.NewCipher(key) 26 | if err != nil { 27 | return false 28 | } 29 | 30 | return cmac.Verify(mac, msg, c, tagsize) 31 | } 32 | 33 | // New returns a hash.Hash computing the AES-CMAC checksum. 34 | func New(key []byte) (hash.Hash, error) { 35 | return NewWithTagSize(key, aes.BlockSize) 36 | } 37 | 38 | // NewWithTagSize returns a hash.Hash computing the AES-CMAC checksum with the 39 | // given tag size. The tag size must between the 1 and the cipher's block size. 40 | func NewWithTagSize(key []byte, tagsize int) (hash.Hash, error) { 41 | c, err := aes.NewCipher(key) 42 | if err != nil { 43 | return nil, err 44 | } 45 | 46 | return cmac.NewWithTagSize(c, tagsize) 47 | } 48 | -------------------------------------------------------------------------------- /skein512/sbox.go: -------------------------------------------------------------------------------- 1 | package skein512 2 | 3 | // Argument types (in the order they must be used). 4 | const ( 5 | keyArg uint64 = 0 6 | configArg uint64 = 4 7 | personArg uint64 = 8 8 | publicKeyArg uint64 = 12 9 | keyIDArg uint64 = 16 10 | nonceArg uint64 = 20 11 | messageArg uint64 = 48 12 | outputArg uint64 = 63 13 | ) 14 | 15 | const ( 16 | firstBlockFlag uint64 = 1 << 62 17 | lastBlockFlag uint64 = 1 << 63 18 | ) 19 | 20 | var schemaId = []byte{'S', 'H', 'A', '3', 1, 0, 0, 0} 21 | var outTweak = [2]uint64{8, outputArg<<56 | firstBlockFlag | lastBlockFlag} 22 | 23 | // Precomputed initial values of state after configuration for unkeyed hashing. 24 | var iv224 = [8]uint64{ 25 | 0xCCD0616248677224, 0xCBA65CF3A92339EF, 0x8CCD69D652FF4B64, 0x398AED7B3AB890B4, 26 | 0x0F59D1B1457D2BD0, 0x6776FE6575D4EB3D, 0x99FBC70E997413E9, 0x9E2CFCCFE1C41EF7, 27 | } 28 | 29 | var iv256 = [8]uint64{ 30 | 0xCCD044A12FDB3E13, 0xE83590301A79A9EB, 0x55AEA0614F816E6F, 0x2A2767A4AE9B94DB, 31 | 0xEC06025E74DD7683, 0xE7A436CDC4746251, 0xC36FBAF9393AD185, 0x3EEDBA1833EDFC13, 32 | } 33 | 34 | var iv384 = [8]uint64{ 35 | 0xA3F6C6BF3A75EF5F, 0xB0FEF9CCFD84FAA4, 0x9D77DD663D770CFE, 0xD798CBF3B468FDDA, 36 | 0x1BC4A6668A0E4465, 0x7ED7D434E5807407, 0x548FC1ACD4EC44D6, 0x266E17546AA18FF8, 37 | } 38 | 39 | var iv512 = [8]uint64{ 40 | 0x4903ADFF749C51CE, 0x0D95DE399746DF03, 0x8FD1934127C79BCE, 0x9A255629FF352CB1, 41 | 0x5DB62599DF6CA7B0, 0xEABE394CA9D5C3F4, 0x991112C71A75B523, 0xAE18A40B660FCC33, 42 | } 43 | -------------------------------------------------------------------------------- /pmac/aes/pmac.go: -------------------------------------------------------------------------------- 1 | // Package aes implements the PMAC MAC with the AES. 2 | // AES-PMAC is specified in RFC 4493 and RFC 4494. 3 | package aes 4 | 5 | import ( 6 | "hash" 7 | "crypto/aes" 8 | 9 | "github.com/deatil/go-hash/pmac" 10 | ) 11 | 12 | // Sum computes the AES-PMAC checksum with the given tagsize of msg using the cipher.Block. 13 | func Sum(msg, key []byte, tagsize int) ([]byte, error) { 14 | c, err := aes.NewCipher(key) 15 | if err != nil { 16 | return nil, err 17 | } 18 | 19 | return pmac.Sum(msg, c, tagsize) 20 | } 21 | 22 | // Verify computes the AES-PMAC checksum with the given tagsize of msg and compares 23 | // it with the given mac. This functions returns true if and only if the given mac 24 | // is equal to the computed one. 25 | func Verify(mac, msg, key []byte, tagsize int) bool { 26 | c, err := aes.NewCipher(key) 27 | if err != nil { 28 | return false 29 | } 30 | 31 | return pmac.Verify(mac, msg, c, tagsize) 32 | } 33 | 34 | // New returns a hash.Hash computing the AES-PMAC checksum. 35 | func New(key []byte) (hash.Hash, error) { 36 | return NewWithTagSize(key, aes.BlockSize) 37 | } 38 | 39 | // NewWithTagSize returns a hash.Hash computing the AES-PMAC checksum with the 40 | // given tag size. The tag size must between the 1 and the cipher's block size. 41 | func NewWithTagSize(key []byte, tagsize int) (hash.Hash, error) { 42 | c, err := aes.NewCipher(key) 43 | if err != nil { 44 | return nil, err 45 | } 46 | 47 | return pmac.NewWithTagSize(c, tagsize) 48 | } 49 | -------------------------------------------------------------------------------- /vmac/utils.go: -------------------------------------------------------------------------------- 1 | package vmac 2 | 3 | import ( 4 | "math/big" 5 | ) 6 | 7 | func nh(k, m []byte) []byte { 8 | t := len(m) / 8 9 | y := big.NewInt(0) 10 | 11 | for i := 0; i < t; i += 2 { 12 | mi := bytesToBigint(m[i*8 : (i+1)*8]) 13 | ki := bytesToBigint(k[i*8 : (i+1)*8]) 14 | mj := bytesToBigint(m[(i+1)*8 : (i+2)*8]) 15 | kj := bytesToBigint(k[(i+1)*8 : (i+2)*8]) 16 | sumi := new(big.Int).Add(mi, ki) 17 | sumi.Mod(sumi, m64) 18 | sumj := new(big.Int).Add(mj, kj) 19 | sumj.Mod(sumj, m64) 20 | prod := new(big.Int).Mul(sumi, sumj) 21 | prod.Mod(prod, m128) 22 | y.Add(y, prod) 23 | y.Mod(y, m128) 24 | } 25 | y.Mod(y, m126) 26 | 27 | Y := make([]byte, 16) 28 | copy(Y[16-len(y.Bytes()):], y.Bytes()) 29 | return Y 30 | } 31 | 32 | func bytesToBigint(b []byte) *big.Int { 33 | return new(big.Int).SetBytes(b) 34 | } 35 | 36 | // Zero pad s to a multiple of 16 bytes 37 | func zeroPad(s []byte) []byte { 38 | r := len(s) % 16 39 | if r != 0 { 40 | t := make([]byte, len(s)+16-r) 41 | copy(t, s) 42 | s = t 43 | } 44 | return s 45 | } 46 | 47 | func endianSwap(s []byte) []byte { 48 | t := make([]byte, len(s)) 49 | for i := 0; i < len(s); i += 8 { 50 | t[i] = s[i+7] 51 | t[i+1] = s[i+6] 52 | t[i+2] = s[i+5] 53 | t[i+3] = s[i+4] 54 | t[i+4] = s[i+3] 55 | t[i+5] = s[i+2] 56 | t[i+6] = s[i+1] 57 | t[i+7] = s[i] 58 | } 59 | 60 | return t 61 | } 62 | -------------------------------------------------------------------------------- /hash/hash_sum.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "io" 5 | "hash" 6 | ) 7 | 8 | // Write 9 | func (this Hash) Write(p []byte) Hash { 10 | this.hash.Write(p) 11 | 12 | return this 13 | } 14 | 15 | // WriteString 16 | func (this Hash) WriteString(s string) Hash { 17 | p := []byte(s) 18 | this.hash.Write(p) 19 | 20 | return this 21 | } 22 | 23 | // WriteReader 24 | func (this Hash) WriteReader(reader io.Reader) Hash { 25 | h := this.hash 26 | 27 | _, err := io.Copy(h, reader) 28 | if err != nil { 29 | this.Error = err 30 | 31 | return this 32 | } 33 | 34 | this.hash = h 35 | 36 | return this 37 | } 38 | 39 | // Sum 40 | func (this Hash) Sum(in []byte) Hash { 41 | this.data = this.hash.Sum(in) 42 | 43 | return this 44 | } 45 | 46 | // Sum32 47 | func (this Hash) Sum32() uint32 { 48 | if hash32, ok := this.hash.(hash.Hash32); ok { 49 | return hash32.Sum32() 50 | } 51 | 52 | return 0 53 | } 54 | 55 | // Sum64 56 | func (this Hash) Sum64() uint64 { 57 | if hash64, ok := this.hash.(hash.Hash64); ok { 58 | return hash64.Sum64() 59 | } 60 | 61 | return 0 62 | } 63 | 64 | // Reset 65 | func (this Hash) Reset() Hash { 66 | this.hash.Reset() 67 | 68 | return this 69 | } 70 | 71 | // Size 72 | func (this Hash) Size() int { 73 | return this.hash.Size() 74 | } 75 | 76 | // BlockSize 77 | func (this Hash) BlockSize() int { 78 | return this.hash.BlockSize() 79 | } 80 | 81 | // Hash 82 | func (this Hash) Hash() hash.Hash { 83 | return this.hash 84 | } 85 | -------------------------------------------------------------------------------- /hash/hash_from.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "io" 5 | "bytes" 6 | ) 7 | 8 | // 字节 9 | func (this Hash) FromBytes(data []byte) Hash { 10 | this.data = data 11 | 12 | return this 13 | } 14 | 15 | // 字节 16 | func FromBytes(data []byte) Hash { 17 | return defaultHash.FromBytes(data) 18 | } 19 | 20 | // 字符 21 | func (this Hash) FromString(data string) Hash { 22 | this.data = []byte(data) 23 | 24 | return this 25 | } 26 | 27 | // 字符 28 | func FromString(data string) Hash { 29 | return defaultHash.FromString(data) 30 | } 31 | 32 | // FromBase64String 33 | func (this Hash) FromBase64String(data string) Hash { 34 | this.data, this.Error = this.Base64Decode(data) 35 | 36 | return this 37 | } 38 | 39 | // FromBase64String 40 | func FromBase64String(data string) Hash { 41 | return defaultHash.FromBase64String(data) 42 | } 43 | 44 | // FromHexString 45 | func (this Hash) FromHexString(data string) Hash { 46 | this.data, this.Error = this.HexDecode(data) 47 | 48 | return this 49 | } 50 | 51 | // FromHexString 52 | func FromHexString(data string) Hash { 53 | return defaultHash.FromHexString(data) 54 | } 55 | 56 | // FromReader 57 | func (this Hash) FromReader(reader io.Reader) Hash { 58 | buf := bytes.NewBuffer(nil) 59 | 60 | // 保存 61 | if _, err := io.Copy(buf, reader); err != nil { 62 | this.Error = err 63 | 64 | return this 65 | } 66 | 67 | this.data = buf.Bytes() 68 | return this 69 | } 70 | 71 | // FromReader 72 | func FromReader(reader io.Reader) Hash { 73 | return defaultHash.FromReader(reader) 74 | } 75 | -------------------------------------------------------------------------------- /sm3/block.go: -------------------------------------------------------------------------------- 1 | package sm3 2 | 3 | func blockGeneric(d *digest, data []byte) { 4 | var W [68]uint32 5 | var SS1, SS2, TT1, TT2 uint32 6 | 7 | var j int32 8 | 9 | A := d.s[0] 10 | B := d.s[1] 11 | C := d.s[2] 12 | D := d.s[3] 13 | E := d.s[4] 14 | F := d.s[5] 15 | G := d.s[6] 16 | H := d.s[7] 17 | 18 | for j = 0; j < 16; j++ { 19 | W[j] = GETU32(data[j*4:]) 20 | } 21 | 22 | for ; j < 68; j++ { 23 | W[j] = P1(W[j - 16] ^ W[j - 9] ^ ROTL(W[j - 3], 15)) ^ 24 | ROTL(W[j - 13], 7) ^ W[j - 6] 25 | } 26 | 27 | for j = 0; j < 16; j++ { 28 | SS1 = ROTL((ROTL(A, 12) + E + tSbox[j]), 7) 29 | SS2 = SS1 ^ ROTL(A, 12) 30 | 31 | TT1 = FF00(A, B, C) + D + SS2 + (W[j] ^ W[j + 4]) 32 | TT2 = GG00(E, F, G) + H + SS1 + W[j] 33 | 34 | D = C 35 | C = ROTL(B, 9) 36 | B = A 37 | A = TT1 38 | H = G 39 | G = ROTL(F, 19) 40 | F = E 41 | E = P0(TT2) 42 | } 43 | 44 | for ; j < 64; j++ { 45 | SS1 = ROTL((ROTL(A, 12) + E + tSbox[j]), 7) 46 | SS2 = SS1 ^ ROTL(A, 12) 47 | 48 | TT1 = FF16(A, B, C) + D + SS2 + (W[j] ^ W[j + 4]) 49 | TT2 = GG16(E, F, G) + H + SS1 + W[j] 50 | 51 | D = C 52 | C = ROTL(B, 9) 53 | B = A 54 | A = TT1 55 | H = G 56 | G = ROTL(F, 19) 57 | F = E 58 | E = P0(TT2) 59 | } 60 | 61 | d.s[0] ^= A 62 | d.s[1] ^= B 63 | d.s[2] ^= C 64 | d.s[3] ^= D 65 | d.s[4] ^= E 66 | d.s[5] ^= F 67 | d.s[6] ^= G 68 | d.s[7] ^= H 69 | } 70 | -------------------------------------------------------------------------------- /hash/crc16_test.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "fmt" 5 | "strings" 6 | "testing" 7 | ) 8 | 9 | var crc16X25Tests = []struct { 10 | input string 11 | output string 12 | }{ 13 | {"sdfgsdgfsdfg123132", "FA73"}, 14 | {"dfg.;kp[jewijr0-34ls", "4938"}, 15 | {"123123", "C56A"}, 16 | } 17 | 18 | func Test_CRC16_X25(t *testing.T) { 19 | assert := assertT(t) 20 | assertError := assertErrorT(t) 21 | 22 | for index, test := range crc16X25Tests { 23 | e := FromString(test.input).CRC16_X25() 24 | 25 | t.Run(fmt.Sprintf("CRC16_X25_test_%d", index), func(t *testing.T) { 26 | assertError(e.Error, "CRC16_X25") 27 | 28 | crcData := LeftCRCPadding(strings.ToUpper(e.ToHexString()), 4) 29 | assert(test.output, crcData, "CRC16_X25") 30 | }) 31 | } 32 | } 33 | 34 | // ==================== 35 | 36 | var crc16ModbusTests = []struct { 37 | input string 38 | output string 39 | }{ 40 | {"sdfgsdgfsdfg123132", "381B"}, 41 | {"dfg.;kp[jewijr0-34ls", "2D12"}, 42 | {"123123", "0036"}, 43 | } 44 | 45 | func Test_CRC16_Modbus(t *testing.T) { 46 | assert := assertT(t) 47 | assertError := assertErrorT(t) 48 | 49 | for index, test := range crc16ModbusTests { 50 | e := FromString(test.input).CRC16_Modbus() 51 | 52 | t.Run(fmt.Sprintf("CRC16_Modbus_test_%d", index), func(t *testing.T) { 53 | assertError(e.Error, "CRC16_Modbus") 54 | 55 | crcData := LeftCRCPadding(strings.ToUpper(e.ToHexString()), 4) 56 | assert(test.output, crcData, "CRC16_Modbus") 57 | }) 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /bmw/bmw.go: -------------------------------------------------------------------------------- 1 | package bmw 2 | 3 | import ( 4 | "hash" 5 | ) 6 | 7 | // New224 returns a new hash.Hash computing the bmw-224 checksum 8 | func New224() hash.Hash { 9 | return newDigest256(initVal224, Size224) 10 | } 11 | 12 | // New256 returns a new hash.Hash computing the bmw-256 checksum 13 | func New256() hash.Hash { 14 | return newDigest256(initVal256, Size256) 15 | } 16 | 17 | // New384 returns a new hash.Hash computing the bmw-384 checksum 18 | func New384() hash.Hash { 19 | return newDigest512(initVal384, Size384) 20 | } 21 | 22 | // New512 returns a new hash.Hash computing the bmw-512 checksum 23 | func New512() hash.Hash { 24 | return newDigest512(initVal512, Size512) 25 | } 26 | 27 | // Sum224 returns the bmw-224 checksum of the data. 28 | func Sum224(data []byte) (out [Size224]byte) { 29 | h := New224() 30 | h.Write(data) 31 | sum := h.Sum(nil) 32 | 33 | copy(out[:], sum) 34 | return 35 | } 36 | 37 | // Sum256 returns the bmw-256 checksum of the data. 38 | func Sum256(data []byte) (out [Size256]byte) { 39 | h := New256() 40 | h.Write(data) 41 | sum := h.Sum(nil) 42 | 43 | copy(out[:], sum) 44 | return 45 | } 46 | 47 | // Sum384 returns the bmw-384 checksum of the data. 48 | func Sum384(data []byte) (out [Size384]byte) { 49 | h := New384() 50 | h.Write(data) 51 | sum := h.Sum(nil) 52 | 53 | copy(out[:], sum) 54 | return 55 | } 56 | 57 | // Sum512 returns the bmw-512 checksum of the data. 58 | func Sum512(data []byte) (out [Size512]byte) { 59 | h := New512() 60 | h.Write(data) 61 | sum := h.Sum(nil) 62 | 63 | copy(out[:], sum) 64 | return 65 | } 66 | -------------------------------------------------------------------------------- /hamsi/hamsi.go: -------------------------------------------------------------------------------- 1 | package hamsi 2 | 3 | import ( 4 | "hash" 5 | ) 6 | 7 | // New224 returns a new hash.Hash computing the hamsi-224 checksum 8 | func New224() hash.Hash { 9 | return newDigest256(iv224, Size224) 10 | } 11 | 12 | // New256 returns a new hash.Hash computing the hamsi-256 checksum 13 | func New256() hash.Hash { 14 | return newDigest256(iv256, Size256) 15 | } 16 | 17 | // New384 returns a new hash.Hash computing the hamsi-384 checksum 18 | func New384() hash.Hash { 19 | return newDigest512(iv384, Size384) 20 | } 21 | 22 | // New512 returns a new hash.Hash computing the hamsi-512 checksum 23 | func New512() hash.Hash { 24 | return newDigest512(iv512, Size512) 25 | } 26 | 27 | // Sum224 returns the hamsi-224 checksum of the data. 28 | func Sum224(data []byte) (out [Size224]byte) { 29 | h := New224() 30 | h.Write(data) 31 | sum := h.Sum(nil) 32 | 33 | copy(out[:], sum) 34 | return 35 | } 36 | 37 | // Sum256 returns the hamsi-256 checksum of the data. 38 | func Sum256(data []byte) (out [Size256]byte) { 39 | h := New256() 40 | h.Write(data) 41 | sum := h.Sum(nil) 42 | 43 | copy(out[:], sum) 44 | return 45 | } 46 | 47 | // Sum384 returns the hamsi-384 checksum of the data. 48 | func Sum384(data []byte) (out [Size384]byte) { 49 | h := New384() 50 | h.Write(data) 51 | sum := h.Sum(nil) 52 | 53 | copy(out[:], sum) 54 | return 55 | } 56 | 57 | // Sum512 returns the hamsi-512 checksum of the data. 58 | func Sum512(data []byte) (out [Size512]byte) { 59 | h := New512() 60 | h.Write(data) 61 | sum := h.Sum(nil) 62 | 63 | copy(out[:], sum) 64 | return 65 | } 66 | -------------------------------------------------------------------------------- /fugue/fugue.go: -------------------------------------------------------------------------------- 1 | package fugue 2 | 3 | import ( 4 | "hash" 5 | ) 6 | 7 | // New224 returns a new hash.Hash computing the fugue-224 checksum 8 | func New224() hash.Hash { 9 | return newDigest(initVal224, Size224) 10 | } 11 | 12 | // New256 returns a new hash.Hash computing the fugue-256 checksum 13 | func New256() hash.Hash { 14 | return newDigest(initVal256, Size256) 15 | } 16 | 17 | // New384 returns a new hash.Hash computing the fugue-384 checksum 18 | func New384() hash.Hash { 19 | return newDigest(initVal384, Size384) 20 | } 21 | 22 | // New512 returns a new hash.Hash computing the fugue-512 checksum 23 | func New512() hash.Hash { 24 | return newDigest(initVal512, Size512) 25 | } 26 | 27 | // Sum224 returns the fugue-224 checksum of the data. 28 | func Sum224(data []byte) (out [Size224]byte) { 29 | h := New224() 30 | h.Write(data) 31 | sum := h.Sum(nil) 32 | 33 | copy(out[:], sum) 34 | return 35 | } 36 | 37 | // Sum256 returns the fugue-256 checksum of the data. 38 | func Sum256(data []byte) (out [Size256]byte) { 39 | h := New256() 40 | h.Write(data) 41 | sum := h.Sum(nil) 42 | 43 | copy(out[:], sum) 44 | return 45 | } 46 | 47 | // Sum384 returns the fugue-384 checksum of the data. 48 | func Sum384(data []byte) (out [Size384]byte) { 49 | h := New384() 50 | h.Write(data) 51 | sum := h.Sum(nil) 52 | 53 | copy(out[:], sum) 54 | return 55 | } 56 | 57 | // Sum512 returns the fugue-512 checksum of the data. 58 | func Sum512(data []byte) (out [Size512]byte) { 59 | h := New512() 60 | h.Write(data) 61 | sum := h.Sum(nil) 62 | 63 | copy(out[:], sum) 64 | return 65 | } 66 | -------------------------------------------------------------------------------- /luffa/luffa.go: -------------------------------------------------------------------------------- 1 | package luffa 2 | 3 | import ( 4 | "hash" 5 | ) 6 | 7 | // New224 returns a new hash.Hash computing the luffa-224 checksum 8 | func New224() hash.Hash { 9 | return newDigest256(Size224) 10 | } 11 | 12 | // New256 returns a new hash.Hash computing the luffa-256 checksum 13 | func New256() hash.Hash { 14 | return newDigest256(Size256) 15 | } 16 | 17 | // Sum224 returns the luffa-224 checksum of the data. 18 | func Sum224(data []byte) (out [Size224]byte) { 19 | h := New224() 20 | h.Write(data) 21 | sum := h.Sum(nil) 22 | 23 | copy(out[:], sum) 24 | return 25 | } 26 | 27 | // Sum256 returns the luffa-256 checksum of the data. 28 | func Sum256(data []byte) (out [Size256]byte) { 29 | h := New256() 30 | h.Write(data) 31 | sum := h.Sum(nil) 32 | 33 | copy(out[:], sum) 34 | return 35 | } 36 | 37 | // ========= 38 | 39 | // New384 returns a new hash.Hash computing the luffa-384 checksum 40 | func New384() hash.Hash { 41 | return newDigest384() 42 | } 43 | 44 | // Sum384 returns the luffa-384 checksum of the data. 45 | func Sum384(data []byte) (out [Size384]byte) { 46 | h := New384() 47 | h.Write(data) 48 | sum := h.Sum(nil) 49 | 50 | copy(out[:], sum) 51 | return 52 | } 53 | 54 | // ========= 55 | 56 | // New512 returns a new hash.Hash computing the luffa-512 checksum 57 | func New512() hash.Hash { 58 | return newDigest512() 59 | } 60 | 61 | // Sum512 returns the luffa-512 checksum of the data. 62 | func Sum512(data []byte) (out [Size512]byte) { 63 | h := New512() 64 | h.Write(data) 65 | sum := h.Sum(nil) 66 | 67 | copy(out[:], sum) 68 | return 69 | } 70 | 71 | -------------------------------------------------------------------------------- /simd/simd.go: -------------------------------------------------------------------------------- 1 | package simd 2 | 3 | import ( 4 | "hash" 5 | ) 6 | 7 | // New224 returns a new hash.Hash computing the SIMD-224 checksum 8 | func New224() hash.Hash { 9 | return newDigest256(Size224, iv224) 10 | } 11 | 12 | // New256 returns a new hash.Hash computing the SIMD-256 checksum 13 | func New256() hash.Hash { 14 | return newDigest256(Size256, iv256) 15 | } 16 | 17 | // Sum224 returns the SIMD-224 checksum of the data. 18 | func Sum224(data []byte) (out [Size224]byte) { 19 | h := New224() 20 | h.Write(data) 21 | sum := h.Sum(nil) 22 | 23 | copy(out[:], sum) 24 | return 25 | } 26 | 27 | // Sum256 returns the SIMD-256 checksum of the data. 28 | func Sum256(data []byte) (out [Size256]byte) { 29 | h := New256() 30 | h.Write(data) 31 | sum := h.Sum(nil) 32 | 33 | copy(out[:], sum) 34 | return 35 | } 36 | 37 | // ============ 38 | 39 | // New384 returns a new hash.Hash computing the SIMD-384 checksum 40 | func New384() hash.Hash { 41 | return newDigest512(Size384, iv384) 42 | } 43 | 44 | // New512 returns a new hash.Hash computing the SIMD-512 checksum 45 | func New512() hash.Hash { 46 | return newDigest512(Size512, iv512) 47 | } 48 | 49 | // Sum384 returns the SIMD-384 checksum of the data. 50 | func Sum384(data []byte) (out [Size384]byte) { 51 | h := New384() 52 | h.Write(data) 53 | sum := h.Sum(nil) 54 | 55 | copy(out[:], sum) 56 | return 57 | } 58 | 59 | // Sum512 returns the SIMD-512 checksum of the data. 60 | func Sum512(data []byte) (out [Size512]byte) { 61 | h := New512() 62 | h.Write(data) 63 | sum := h.Sum(nil) 64 | 65 | copy(out[:], sum) 66 | return 67 | } 68 | -------------------------------------------------------------------------------- /xxhash/xxhash64/utils.go: -------------------------------------------------------------------------------- 1 | package xxhash64 2 | 3 | import ( 4 | "math/bits" 5 | "encoding/binary" 6 | ) 7 | 8 | func getu32(ptr []byte) uint32 { 9 | return binary.LittleEndian.Uint32(ptr) 10 | } 11 | 12 | func getu64(ptr []byte) uint64 { 13 | return binary.LittleEndian.Uint64(ptr) 14 | } 15 | 16 | func putu64(ptr []byte, a uint64) { 17 | binary.LittleEndian.PutUint64(ptr, a) 18 | } 19 | 20 | func putu64be(ptr []byte, a uint64) { 21 | binary.BigEndian.PutUint64(ptr, a) 22 | } 23 | 24 | func bytesToUint64s(b []byte) []uint64 { 25 | size := len(b) / 8 26 | dst := make([]uint64, size) 27 | 28 | for i := 0; i < size; i++ { 29 | j := i * 8 30 | 31 | dst[i] = binary.LittleEndian.Uint64(b[j:]) 32 | } 33 | 34 | return dst 35 | } 36 | 37 | func uint64sToBytes(w []uint64) []byte { 38 | size := len(w) * 8 39 | dst := make([]byte, size) 40 | 41 | for i := 0; i < len(w); i++ { 42 | j := i * 8 43 | 44 | binary.LittleEndian.PutUint64(dst[j:], w[i]) 45 | } 46 | 47 | return dst 48 | } 49 | 50 | func rotl(x, n uint64) uint64 { 51 | return bits.RotateLeft64(x, int(n)) 52 | } 53 | 54 | func round(acc, input uint64) uint64 { 55 | acc += input * prime[1] 56 | acc = rotl(acc, 31) 57 | acc *= prime[0] 58 | return acc 59 | } 60 | 61 | func mergeRound(acc, val uint64) uint64 { 62 | val = round(0, val) 63 | acc ^= val 64 | acc = acc * prime[0] + prime[3] 65 | return acc 66 | } 67 | 68 | func avalanche(h64 uint64) uint64 { 69 | h64 ^= h64 >> 33 70 | h64 *= prime[1] 71 | h64 ^= h64 >> 29 72 | h64 *= prime[2] 73 | h64 ^= h64 >> 32 74 | return h64 75 | } 76 | -------------------------------------------------------------------------------- /shavite/shavite.go: -------------------------------------------------------------------------------- 1 | package shavite 2 | 3 | import ( 4 | "hash" 5 | ) 6 | 7 | // New224 returns a new hash.Hash computing the SHAvite-224 checksum 8 | func New224() hash.Hash { 9 | return newDigest256(Size224, initVal224) 10 | } 11 | 12 | // New256 returns a new hash.Hash computing the SHAvite-256 checksum 13 | func New256() hash.Hash { 14 | return newDigest256(Size256, initVal256) 15 | } 16 | 17 | // New384 returns a new hash.Hash computing the SHAvite-384 checksum 18 | func New384() hash.Hash { 19 | return newDigest512(Size384, initVal384) 20 | } 21 | 22 | // New512 returns a new hash.Hash computing the SHAvite-512 checksum 23 | func New512() hash.Hash { 24 | return newDigest512(Size512, initVal512) 25 | } 26 | 27 | // Sum224 returns the SHAvite-224 checksum of the data. 28 | func Sum224(data []byte) (out [Size224]byte) { 29 | h := New224() 30 | h.Write(data) 31 | sum := h.Sum(nil) 32 | 33 | copy(out[:], sum) 34 | return 35 | } 36 | 37 | // Sum256 returns the SHAvite-256 checksum of the data. 38 | func Sum256(data []byte) (out [Size256]byte) { 39 | h := New256() 40 | h.Write(data) 41 | sum := h.Sum(nil) 42 | 43 | copy(out[:], sum) 44 | return 45 | } 46 | 47 | // Sum384 returns the SHAvite-384 checksum of the data. 48 | func Sum384(data []byte) (out [Size384]byte) { 49 | h := New384() 50 | h.Write(data) 51 | sum := h.Sum(nil) 52 | 53 | copy(out[:], sum) 54 | return 55 | } 56 | 57 | // Sum512 returns the SHAvite-512 checksum of the data. 58 | func Sum512(data []byte) (out [Size512]byte) { 59 | h := New512() 60 | h.Write(data) 61 | sum := h.Sum(nil) 62 | 63 | copy(out[:], sum) 64 | return 65 | } 66 | -------------------------------------------------------------------------------- /xxhash/xxh3/xxh3_128.go: -------------------------------------------------------------------------------- 1 | package xxh3 2 | 3 | import( 4 | "fmt" 5 | ) 6 | 7 | // New128 returns a new Hash128 computing the XXH3-128 checksum 8 | func New128() Hash128 { 9 | return New128WithSeed(0) 10 | } 11 | 12 | // New128WithSecret returns a new Hash128 computing the XXH3-128 checksum 13 | func New128WithSecret(secret []byte) (Hash128, error) { 14 | if len(secret) < SECRET_SIZE_MIN { 15 | return nil, fmt.Errorf("secret too short") 16 | } 17 | 18 | return newDigest128(0, secret), nil 19 | } 20 | 21 | // New128WithSeed returns a new Hash128 computing the XXH3-128 checksum 22 | func New128WithSeed(seed uint64) Hash128 { 23 | secret := make([]byte, SECRET_DEFAULT_SIZE) 24 | GenCustomSecret(secret, seed) 25 | 26 | return newDigest128(seed, secret) 27 | } 28 | 29 | // Sum128 returns the 128bits Hash value. 30 | func Sum128(input []byte) (out [Size128]byte) { 31 | d := New128() 32 | d.Write(input) 33 | sum := d.Sum(nil) 34 | 35 | copy(out[:], sum) 36 | return 37 | } 38 | 39 | // Sum128WithSeed returns the 128bits Hash value. 40 | func Sum128WithSeed(input []byte, seed uint64) (out [Size128]byte) { 41 | d := New128WithSeed(seed) 42 | d.Write(input) 43 | sum := d.Sum(nil) 44 | 45 | copy(out[:], sum) 46 | return 47 | } 48 | 49 | // Checksum128 returns the Uint128 value. 50 | func Checksum128(input []byte) Uint128 { 51 | d := New128() 52 | d.Write(input) 53 | 54 | return d.Sum128() 55 | } 56 | 57 | // Checksum128WithSeed returns the Uint128 value. 58 | func Checksum128WithSeed(input []byte, seed uint64) Uint128 { 59 | d := New128WithSeed(seed) 60 | d.Write(input) 61 | 62 | return d.Sum128() 63 | } 64 | -------------------------------------------------------------------------------- /makwa/params.go: -------------------------------------------------------------------------------- 1 | package makwa 2 | 3 | import ( 4 | "hash" 5 | "math/big" 6 | "crypto/rand" 7 | "crypto/sha256" 8 | ) 9 | 10 | // PublicParameters are the public parameters associated with Makwa. 11 | type PublicParameters struct { 12 | N *big.Int 13 | Hash func() hash.Hash 14 | } 15 | 16 | // ModulusID returns a fingerprint of the modulus. 17 | func (p PublicParameters) ModulusID() []byte { 18 | return kdf(p.Hash, p.N.Bytes(), 8) 19 | } 20 | 21 | // PrivateParameters are the private parameters associated with Makwa. 22 | type PrivateParameters struct { 23 | PublicParameters 24 | P, Q *big.Int 25 | } 26 | 27 | // GenerateParameters generates a random Makwa modulus of the given size. 28 | func GenerateParameters(bits int) (*PrivateParameters, error) { 29 | pBits := (bits + 1) >> 1 30 | qBits := bits - pBits 31 | 32 | p, err := genPrime(pBits) 33 | if err != nil { 34 | return nil, err 35 | } 36 | 37 | q, err := genPrime(qBits) 38 | if err != nil { 39 | return nil, err 40 | } 41 | 42 | return &PrivateParameters{ 43 | PublicParameters: PublicParameters{ 44 | N: new(big.Int).Mul(p, q), 45 | Hash: sha256.New, 46 | }, 47 | P: p, 48 | Q: q, 49 | }, nil 50 | } 51 | 52 | var ( 53 | three = big.NewInt(3) 54 | four = big.NewInt(4) 55 | ) 56 | 57 | func genPrime(bits int) (*big.Int, error) { 58 | for { 59 | n, err := rand.Prime(rand.Reader, bits) 60 | if err != nil { 61 | return nil, err 62 | } 63 | if new(big.Int).Mod(n, four).Cmp(three) == 0 { 64 | return n, nil 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /xxhash/xxh3/xxh3_64.go: -------------------------------------------------------------------------------- 1 | package xxh3 2 | 3 | import ( 4 | "fmt" 5 | "hash" 6 | ) 7 | 8 | // New64 returns a new hash.Hash64 computing the XXH3-64 checksum 9 | func New64() hash.Hash64 { 10 | return New64WithSeed(0) 11 | } 12 | 13 | // New64WithSecret returns a new hash.Hash64 computing the XXH3-64 checksum 14 | func New64WithSecret(secret []byte) (hash.Hash64, error) { 15 | if len(secret) < SECRET_SIZE_MIN { 16 | return nil, fmt.Errorf("secret too short") 17 | } 18 | 19 | return newDigest64(0, secret), nil 20 | } 21 | 22 | // New64WithSeed returns a new hash.Hash64 computing the XXH3-64 checksum 23 | func New64WithSeed(seed uint64) hash.Hash64 { 24 | secret := make([]byte, SECRET_DEFAULT_SIZE) 25 | GenCustomSecret(secret, seed) 26 | 27 | return newDigest64(seed, secret) 28 | } 29 | 30 | // Sum64 returns the 64bits Hash value. 31 | func Sum64(input []byte) (out [Size64]byte) { 32 | d := New64() 33 | d.Write(input) 34 | sum := d.Sum(nil) 35 | 36 | copy(out[:], sum) 37 | return 38 | } 39 | 40 | // Sum64WithSeed returns the 64bits Hash value. 41 | func Sum64WithSeed(input []byte, seed uint64) (out [Size64]byte) { 42 | d := New64WithSeed(seed) 43 | d.Write(input) 44 | sum := d.Sum(nil) 45 | 46 | copy(out[:], sum) 47 | return 48 | } 49 | 50 | // Checksum64 returns the uint64 value. 51 | func Checksum64(input []byte) uint64 { 52 | d := New64() 53 | d.Write(input) 54 | 55 | return d.Sum64() 56 | } 57 | 58 | // Checksum64WithSeed returns the uint64 value. 59 | func Checksum64WithSeed(input []byte, seed uint64) uint64 { 60 | d := New64WithSeed(seed) 61 | d.Write(input) 62 | 63 | return d.Sum64() 64 | } 65 | -------------------------------------------------------------------------------- /jh2/jh2.go: -------------------------------------------------------------------------------- 1 | package jh2 2 | 3 | import ( 4 | "hash" 5 | ) 6 | 7 | // New224 returns a new hash.Hash computing the JH-224 checksum 8 | func New224() hash.Hash { 9 | return newDigest(Size224, initVal224) 10 | } 11 | 12 | // Sum224 returns the JH-224 checksum of the data. 13 | func Sum224(data []byte) (out [Size224]byte) { 14 | h := New224() 15 | h.Write(data) 16 | sum := h.Sum(nil) 17 | 18 | copy(out[:], sum) 19 | return 20 | } 21 | 22 | // =========== 23 | 24 | // New256 returns a new hash.Hash computing the JH-256 checksum 25 | func New256() hash.Hash { 26 | return newDigest(Size256, initVal256) 27 | } 28 | 29 | // Sum256 returns the JH-256 checksum of the data. 30 | func Sum256(data []byte) (out [Size256]byte) { 31 | h := New256() 32 | h.Write(data) 33 | sum := h.Sum(nil) 34 | 35 | copy(out[:], sum) 36 | return 37 | } 38 | 39 | // =========== 40 | 41 | // New384 returns a new hash.Hash computing the JH-384 checksum 42 | func New384() hash.Hash { 43 | return newDigest(Size384, initVal384) 44 | } 45 | 46 | // Sum384 returns the JH-384 checksum of the data. 47 | func Sum384(data []byte) (out [Size384]byte) { 48 | h := New384() 49 | h.Write(data) 50 | sum := h.Sum(nil) 51 | 52 | copy(out[:], sum) 53 | return 54 | } 55 | 56 | // =========== 57 | 58 | // New512 returns a new hash.Hash computing the JH-512 checksum 59 | func New512() hash.Hash { 60 | return newDigest(Size512, initVal512) 61 | } 62 | 63 | // Sum512 returns the JH-512 checksum of the data. 64 | func Sum512(data []byte) (out [Size512]byte) { 65 | h := New512() 66 | h.Write(data) 67 | sum := h.Sum(nil) 68 | 69 | copy(out[:], sum) 70 | return 71 | } 72 | -------------------------------------------------------------------------------- /groestl/groestl.go: -------------------------------------------------------------------------------- 1 | package groestl 2 | 3 | import ( 4 | "hash" 5 | ) 6 | 7 | // New224 returns a new hash.Hash computing the Groestl checksum 8 | func New224() hash.Hash { 9 | h, _ := New(224) 10 | return h 11 | } 12 | 13 | // New256 returns a new hash.Hash computing the Groestl checksum 14 | func New256() hash.Hash { 15 | h, _ := New(256) 16 | return h 17 | } 18 | 19 | // New384 returns a new hash.Hash computing the Groestl checksum 20 | func New384() hash.Hash { 21 | h, _ := New(384) 22 | return h 23 | } 24 | 25 | // New512 returns a new hash.Hash computing the Groestl checksum 26 | func New512() hash.Hash { 27 | h, _ := New(512) 28 | return h 29 | } 30 | 31 | // Sum224 returns the Groestl-224 checksum of the data. 32 | func Sum224(data []byte) (sum224 [Size224]byte) { 33 | h := New224() 34 | h.Write(data) 35 | sum := h.Sum(nil) 36 | 37 | copy(sum224[:], sum[:Size224]) 38 | return 39 | } 40 | 41 | // Sum256 returns the Groestl-256 checksum of the data. 42 | func Sum256(data []byte) (sum256 [Size256]byte) { 43 | h := New256() 44 | h.Write(data) 45 | sum := h.Sum(nil) 46 | 47 | copy(sum256[:], sum[:Size256]) 48 | return 49 | } 50 | 51 | // Sum384 returns the Groestl-384 checksum of the data. 52 | func Sum384(data []byte) (sum384 [Size384]byte) { 53 | h := New384() 54 | h.Write(data) 55 | sum := h.Sum(nil) 56 | 57 | copy(sum384[:], sum[:Size384]) 58 | return 59 | } 60 | 61 | // Sum512 returns the Groestl-512 checksum of the data. 62 | func Sum512(data []byte) (sum512 [Size512]byte) { 63 | h := New512() 64 | h.Write(data) 65 | sum := h.Sum(nil) 66 | 67 | copy(sum512[:], sum[:Size512]) 68 | return 69 | } 70 | -------------------------------------------------------------------------------- /makwa/digest_test.go: -------------------------------------------------------------------------------- 1 | package makwa_test 2 | 3 | import ( 4 | "bytes" 5 | "testing" 6 | 7 | "github.com/deatil/go-hash/makwa" 8 | ) 9 | 10 | func TestDigestMarshalText(t *testing.T) { 11 | d := &makwa.Digest{ 12 | ModulusID: modulusID, 13 | Hash: hash, 14 | Salt: salt, 15 | WorkFactor: 4096, 16 | PreHash: false, 17 | PostHashLen: 12, 18 | } 19 | 20 | b, err := d.MarshalText() 21 | if err != nil { 22 | t.Fatal(err) 23 | } 24 | 25 | if v, want := string(b), "+RK3n5jz7gs_s211_xycDwiqW2ZkvPeqHZJfjkg_yc6g5u8JOTqxcQoI"; v != want { 26 | t.Errorf("Was %s, but expected %s", v, want) 27 | } 28 | } 29 | 30 | func TestDigestUnmarshalText(t *testing.T) { 31 | d := &makwa.Digest{} 32 | if err := d.UnmarshalText([]byte("+RK3n5jz7gs_s211_xycDwiqW2ZkvPeqHZJfjkg_yc6g5u8JOTqxcQoI")); err != nil { 33 | t.Fatal(err) 34 | } 35 | 36 | if !bytes.Equal(d.ModulusID, modulusID) { 37 | t.Errorf("ModulusID was %x but expected %x", d.ModulusID, modulusID) 38 | } 39 | 40 | if !bytes.Equal(d.Hash, hash) { 41 | t.Errorf("Hash was %x but expected %x", d.Hash, hash) 42 | } 43 | 44 | if !bytes.Equal(d.Salt, salt) { 45 | t.Errorf("Salt was %x but expected %x", d.Salt, salt) 46 | } 47 | 48 | if v, want := d.WorkFactor, 4096; v != want { 49 | t.Errorf("WorkFactor was %v, but expected %v", v, want) 50 | } 51 | 52 | if d.PreHash { 53 | t.Errorf("PreHash was %v, but expected false", d.PreHash) 54 | } 55 | 56 | if v, want := d.PostHashLen, 12; v != want { 57 | t.Errorf("PostHashLen was %v, but expected %v", v, want) 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /hash/hash_use.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "fmt" 5 | "hash" 6 | "strconv" 7 | ) 8 | 9 | // Type Mode 10 | var TypeMode = NewTypeSet[Mode, string](maxMode) 11 | 12 | // Mode type 13 | type Mode uint 14 | 15 | func (this Mode) String() string { 16 | switch this { 17 | default: 18 | if TypeMode.Names().Has(this) { 19 | return (TypeMode.Names().Get(this))() 20 | } 21 | 22 | return "unknown mode value " + strconv.Itoa(int(this)) 23 | } 24 | } 25 | 26 | const ( 27 | unknown Mode = 1 + iota 28 | maxMode 29 | ) 30 | 31 | // 接口 32 | type IHash interface { 33 | // Sum [输入内容, 其他配置] 34 | Sum(data []byte, cfg ...any) ([]byte, error) 35 | 36 | // New 37 | New(cfg ...any) (hash.Hash, error) 38 | } 39 | 40 | // 使用 41 | var UseHash = NewDataSet[Mode, IHash]() 42 | 43 | // 获取方式 44 | func getHash(name Mode) (IHash, error) { 45 | if !UseHash.Has(name) { 46 | err := fmt.Errorf("Hash: Hash type [%s] is error.", name) 47 | return nil, err 48 | } 49 | 50 | newHash := UseHash.Get(name) 51 | 52 | return newHash(), nil 53 | } 54 | 55 | // Sum 56 | func (this Hash) SumBy(name Mode, cfg ...any) Hash { 57 | newHash, err := getHash(name) 58 | if err != nil { 59 | this.Error = err 60 | return this 61 | } 62 | 63 | this.data, this.Error = newHash.Sum(this.data, cfg...) 64 | 65 | return this 66 | } 67 | 68 | // New 69 | func (this Hash) NewBy(name Mode, cfg ...any) Hash { 70 | newHash, err := getHash(name) 71 | if err != nil { 72 | this.Error = err 73 | return this 74 | } 75 | 76 | this.hash, this.Error = newHash.New(cfg...) 77 | 78 | return this 79 | } 80 | -------------------------------------------------------------------------------- /hash/hash.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "hash" 5 | "encoding/hex" 6 | "encoding/base64" 7 | ) 8 | 9 | // 默认 10 | var defaultHash Hash 11 | 12 | func init() { 13 | defaultHash = NewHash() 14 | } 15 | 16 | /** 17 | * hash 18 | * 19 | * @create 2022-3-27 20 | * @author deatil 21 | */ 22 | type Hash struct { 23 | // 数据 24 | data []byte 25 | 26 | // hash 接口 27 | hash hash.Hash 28 | 29 | // 错误 30 | Error error 31 | } 32 | 33 | // 构造函数 34 | func NewHash() Hash { 35 | return Hash{} 36 | } 37 | 38 | // 构造函数 39 | func New() Hash { 40 | return NewHash() 41 | } 42 | 43 | // 构造函数 44 | func Hashing() Hash { 45 | return NewHash() 46 | } 47 | 48 | // Base64 编码 49 | func (this Hash) Base64Encode(src []byte) string { 50 | return base64.StdEncoding.EncodeToString(src) 51 | } 52 | 53 | // Base64 解码 54 | func (this Hash) Base64Decode(s string) ([]byte, error) { 55 | return base64.StdEncoding.DecodeString(s) 56 | } 57 | 58 | // Hex 编码 59 | func (this Hash) HexEncode(src []byte) string { 60 | return hex.EncodeToString(src) 61 | } 62 | 63 | // Hex 解码 64 | func (this Hash) HexDecode(s string) ([]byte, error) { 65 | return hex.DecodeString(s) 66 | } 67 | 68 | // NewHash 69 | func (this Hash) NewHash(h hash.Hash) Hash { 70 | this.hash = h 71 | 72 | return this 73 | } 74 | 75 | // CheckSum 76 | func (this Hash) Checksum(h hash.Hash) Hash { 77 | h.Write(this.data) 78 | 79 | this.data = h.Sum(nil) 80 | 81 | return this 82 | } 83 | 84 | // 自定义方法 85 | func (this Hash) FuncHash(f func([]byte) ([]byte, error)) Hash { 86 | this.data, this.Error = f(this.data) 87 | 88 | return this 89 | } 90 | 91 | // GetError 92 | func (this Hash) GetError() error { 93 | return this.Error 94 | } 95 | -------------------------------------------------------------------------------- /sha0/sha0_test.go: -------------------------------------------------------------------------------- 1 | package sha0 2 | 3 | import ( 4 | "fmt" 5 | "bytes" 6 | "testing" 7 | "encoding/hex" 8 | ) 9 | 10 | func fromHex(s string) []byte { 11 | h, _ := hex.DecodeString(s) 12 | return h 13 | } 14 | 15 | func fromString(s string) []byte { 16 | return []byte(s) 17 | } 18 | 19 | type testData struct { 20 | msg []byte 21 | md []byte 22 | } 23 | 24 | func Test_MillionA_Check(t *testing.T) { 25 | msg := make([]byte, 1000) 26 | for i := 0; i < 1000; i++ { 27 | msg[i] = 'a' 28 | } 29 | 30 | check := "3232affa48628a26653b5aaa44541fd90d690603" 31 | 32 | h := New() 33 | for i := 0; i < 1000; i++ { 34 | h.Write(msg) 35 | } 36 | 37 | got := fmt.Sprintf("%x", h.Sum(nil)) 38 | if got != check { 39 | t.Errorf("fail, got %s, want %s", got, check) 40 | } 41 | } 42 | 43 | func Test_Hash_Check(t *testing.T) { 44 | tests := []testData{ 45 | { 46 | fromString("abc"), 47 | fromHex("0164b8a914cd2a5e74c4f7ff082c4d97f1edf880"), 48 | }, 49 | { 50 | fromString("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"), 51 | fromHex("d2516ee1acfa5baf33dfc1c471e438449ef134c8"), 52 | }, 53 | } 54 | 55 | h := New() 56 | 57 | for i, test := range tests { 58 | h.Reset() 59 | h.Write(test.msg) 60 | sum := h.Sum(nil) 61 | 62 | if !bytes.Equal(sum, test.md) { 63 | t.Errorf("[%d] New fail, got %x, want %x", i, sum, test.md) 64 | } 65 | 66 | // ===== 67 | 68 | sum2 := Sum(test.msg) 69 | 70 | if !bytes.Equal(sum2[:], test.md) { 71 | t.Errorf("[%d] Sum fail, got %x, want %x", i, sum2, test.md) 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /sm3/const_asm.s: -------------------------------------------------------------------------------- 1 | #define T0 0x79cc4519 2 | #define T1 0xf3988a32 3 | #define T2 0xe7311465 4 | #define T3 0xce6228cb 5 | #define T4 0x9cc45197 6 | #define T5 0x3988a32f 7 | #define T6 0x7311465e 8 | #define T7 0xe6228cbc 9 | 10 | #define T8 0xcc451979 11 | #define T9 0x988a32f3 12 | #define T10 0x311465e7 13 | #define T11 0x6228cbce 14 | #define T12 0xc451979c 15 | #define T13 0x88a32f39 16 | #define T14 0x11465e73 17 | #define T15 0x228cbce6 18 | 19 | #define T16 0x9d8a7a87 20 | #define T17 0x3b14f50f 21 | #define T18 0x7629ea1e 22 | #define T19 0xec53d43c 23 | #define T20 0xd8a7a879 24 | #define T21 0xb14f50f3 25 | #define T22 0x629ea1e7 26 | #define T23 0xc53d43ce 27 | 28 | #define T24 0x8a7a879d 29 | #define T25 0x14f50f3b 30 | #define T26 0x29ea1e76 31 | #define T27 0x53d43cec 32 | #define T28 0xa7a879d8 33 | #define T29 0x4f50f3b1 34 | #define T30 0x9ea1e762 35 | #define T31 0x3d43cec5 36 | 37 | #define T32 0x7a879d8a 38 | #define T33 0xf50f3b14 39 | #define T34 0xea1e7629 40 | #define T35 0xd43cec53 41 | #define T36 0xa879d8a7 42 | #define T37 0x50f3b14f 43 | #define T38 0xa1e7629e 44 | #define T39 0x43cec53d 45 | 46 | #define T40 0x879d8a7a 47 | #define T41 0xf3b14f5 48 | #define T42 0x1e7629ea 49 | #define T43 0x3cec53d4 50 | #define T44 0x79d8a7a8 51 | #define T45 0xf3b14f50 52 | #define T46 0xe7629ea1 53 | #define T47 0xcec53d43 54 | 55 | #define T48 0x9d8a7a87 56 | #define T49 0x3b14f50f 57 | #define T50 0x7629ea1e 58 | #define T51 0xec53d43c 59 | #define T52 0xd8a7a879 60 | #define T53 0xb14f50f3 61 | #define T54 0x629ea1e7 62 | #define T55 0xc53d43ce 63 | 64 | #define T56 0x8a7a879d 65 | #define T57 0x14f50f3b 66 | #define T58 0x29ea1e76 67 | #define T59 0x53d43cec 68 | #define T60 0xa7a879d8 69 | #define T61 0x4f50f3b1 70 | #define T62 0x9ea1e762 71 | #define T63 0x3d43cec5 72 | -------------------------------------------------------------------------------- /hash/sm3_test.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | var sm3Tests = []struct { 9 | output string 10 | input string 11 | }{ 12 | {"90d52a2e85631a8d6035262626941fa11b85ce570cec1e3e991e2dd7ed258148", "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd"}, 13 | {"e1c53f367a9c5d19ab6ddd30248a7dafcc607e74e6bcfa52b00e0ba35e470421", "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabc"}, 14 | {"520472cafdaf21d994c5849492ba802459472b5206503389fc81ff73adbec1b4", "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabc"}, 15 | } 16 | 17 | func Test_SM3(t *testing.T) { 18 | assert := assertT(t) 19 | assertError := assertErrorT(t) 20 | 21 | for index, test := range sm3Tests { 22 | e := FromString(test.input).SM3() 23 | 24 | t.Run(fmt.Sprintf("SM3 %d", index), func(t *testing.T) { 25 | assertError(e.Error, "SM3") 26 | assert(test.output, e.ToHexString(), "SM3") 27 | }) 28 | } 29 | } 30 | 31 | func Test_NewSM3(t *testing.T) { 32 | assert := assertT(t) 33 | assertError := assertErrorT(t) 34 | 35 | for index, test := range sm3Tests { 36 | e := FromString("").NewSM3().Write([]byte(test.input)).Sum(nil) 37 | 38 | t.Run(fmt.Sprintf("NewSM3 %d", index), func(t *testing.T) { 39 | assertError(e.Error, "NewSM3") 40 | assert(test.output, e.ToHexString(), "NewSM3") 41 | }) 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /has160/utils.go: -------------------------------------------------------------------------------- 1 | package has160 2 | 3 | import ( 4 | "math/bits" 5 | "encoding/binary" 6 | ) 7 | 8 | func GETU32(ptr []byte) uint32 { 9 | return binary.LittleEndian.Uint32(ptr) 10 | } 11 | 12 | func PUTU32(ptr []byte, a uint32) { 13 | binary.LittleEndian.PutUint32(ptr, a) 14 | } 15 | 16 | func PUTU64(ptr []byte, a uint64) { 17 | binary.LittleEndian.PutUint64(ptr, a) 18 | } 19 | 20 | func bytesToUint32s(b []byte) []uint32 { 21 | size := len(b) / 4 22 | dst := make([]uint32, size) 23 | 24 | for i := 0; i < size; i++ { 25 | j := i * 4 26 | 27 | dst[i] = binary.LittleEndian.Uint32(b[j:]) 28 | } 29 | 30 | return dst 31 | } 32 | 33 | func uint32sToBytes(w []uint32) []byte { 34 | size := len(w) * 4 35 | dst := make([]byte, size) 36 | 37 | for i := 0; i < len(w); i++ { 38 | j := i * 4 39 | 40 | binary.LittleEndian.PutUint32(dst[j:], w[i]) 41 | } 42 | 43 | return dst 44 | } 45 | 46 | func rol(x uint32, n int) uint32 { 47 | return bits.RotateLeft32(x, n) 48 | } 49 | 50 | func f1(h []uint32, a, b, c, d, e int, x []uint32, j int) { 51 | h[e] += rol(h[a], s1[j]) + x[x1[j]] + 0x00000000 + ((h[b] & h[c]) | ((^h[b]) & h[d])) 52 | h[b] = rol(h[b], 10) 53 | } 54 | 55 | func f2(h []uint32, a, b, c, d, e int, x []uint32, j int) { 56 | h[e] += rol(h[a], s1[j]) + x[x2[j]] + 0x5a827999 + (h[b] ^ h[c] ^ h[d]) 57 | h[b] = rol(h[b], 17) 58 | } 59 | 60 | func f3(h []uint32, a, b, c, d, e int, x []uint32, j int) { 61 | h[e] += rol(h[a], s1[j]) + x[x3[j]] + 0x6ed9eba1 + (h[c] ^ (h[b] | (^h[d]))) 62 | h[b] = rol(h[b], 25) 63 | } 64 | 65 | func f4(h []uint32, a, b, c, d, e int, x []uint32, j int) { 66 | h[e] += rol(h[a], s1[j]) + x[x4[j]] + 0x8f1bbcdc + (h[b] ^ h[c] ^ h[d]) 67 | h[b] = rol(h[b], 30) 68 | } 69 | -------------------------------------------------------------------------------- /bmw/utils.go: -------------------------------------------------------------------------------- 1 | package bmw 2 | 3 | import ( 4 | "math/bits" 5 | "encoding/binary" 6 | ) 7 | 8 | func getu32(ptr []byte) uint32 { 9 | return binary.LittleEndian.Uint32(ptr) 10 | } 11 | 12 | func putu32(ptr []byte, a uint32) { 13 | binary.LittleEndian.PutUint32(ptr, a) 14 | } 15 | 16 | func getu64(ptr []byte) uint64 { 17 | return binary.LittleEndian.Uint64(ptr) 18 | } 19 | 20 | func putu64(ptr []byte, a uint64) { 21 | binary.LittleEndian.PutUint64(ptr, a) 22 | } 23 | 24 | func bytesToUint32s(b []byte) []uint32 { 25 | size := len(b) / 4 26 | dst := make([]uint32, size) 27 | 28 | for i := 0; i < size; i++ { 29 | j := i * 4 30 | 31 | dst[i] = binary.LittleEndian.Uint32(b[j:]) 32 | } 33 | 34 | return dst 35 | } 36 | 37 | func uint32sToBytes(w []uint32) []byte { 38 | size := len(w) * 4 39 | dst := make([]byte, size) 40 | 41 | for i := 0; i < len(w); i++ { 42 | j := i * 4 43 | 44 | binary.LittleEndian.PutUint32(dst[j:], w[i]) 45 | } 46 | 47 | return dst 48 | } 49 | 50 | func bytesToUint64s(b []byte) []uint64 { 51 | size := len(b) / 8 52 | dst := make([]uint64, size) 53 | 54 | for i := 0; i < size; i++ { 55 | j := i * 8 56 | 57 | dst[i] = binary.LittleEndian.Uint64(b[j:]) 58 | } 59 | 60 | return dst 61 | } 62 | 63 | func uint64sToBytes(w []uint64) []byte { 64 | size := len(w) * 8 65 | dst := make([]byte, size) 66 | 67 | for i := 0; i < len(w); i++ { 68 | j := i * 8 69 | 70 | binary.LittleEndian.PutUint64(dst[j:], w[i]) 71 | } 72 | 73 | return dst 74 | } 75 | 76 | func circularLeft32(x uint32, n int) uint32 { 77 | return bits.RotateLeft32(x, n) 78 | } 79 | 80 | func circularLeft64(x uint64, n int) uint64 { 81 | return bits.RotateLeft64(x, n) 82 | } 83 | -------------------------------------------------------------------------------- /hash/md5_test.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "fmt" 5 | "bytes" 6 | "testing" 7 | ) 8 | 9 | var md5Tests = []struct { 10 | input string 11 | output string 12 | }{ 13 | {"sdfgsdgfsdfg123132", "f7d9f5d96d7935a47cee64ab0560843d"}, 14 | {"dfg.;kp[jewijr0-34lsd", "808c4183cd07a8f9fdac2dc06107d0d9"}, 15 | {"123123", "4297f44b13955235245b2497399d7a93"}, 16 | } 17 | 18 | func Test_MD5(t *testing.T) { 19 | assert := assertT(t) 20 | assertError := assertErrorT(t) 21 | 22 | for index, test := range md5Tests { 23 | e := FromString(test.input).MD5() 24 | 25 | t.Run(fmt.Sprintf("md5_test_%d", index), func(t *testing.T) { 26 | assertError(e.Error, "MD5") 27 | assert(test.output, e.ToHexString(), "MD5") 28 | }) 29 | } 30 | } 31 | 32 | func Test_NewMD5(t *testing.T) { 33 | assert := assertT(t) 34 | assertError := assertErrorT(t) 35 | 36 | for index, test := range md5Tests { 37 | e := Hashing().NewMD5().Write([]byte(test.input)).Sum(nil) 38 | 39 | t.Run(fmt.Sprintf("NewMD5_test_%d", index), func(t *testing.T) { 40 | assertError(e.Error, "NewMD5") 41 | assert(test.output, e.ToHexString(), "NewMD5") 42 | }) 43 | } 44 | } 45 | 46 | func Test_NewMD5_WriteReader(t *testing.T) { 47 | assert := assertT(t) 48 | assertError := assertErrorT(t) 49 | 50 | for index, test := range md5Tests { 51 | buf := bytes.NewBuffer([]byte(test.input)) 52 | 53 | e := Hashing().NewMD5().WriteReader(buf).Sum(nil) 54 | 55 | t.Run(fmt.Sprintf("NewMD5_WriteReader_test_%d", index), func(t *testing.T) { 56 | assertError(e.Error, "NewMD5_WriteReader") 57 | assert(test.output, e.ToHexString(), "NewMD5_WriteReader") 58 | }) 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /kupyna/kmac256.go: -------------------------------------------------------------------------------- 1 | package kupyna 2 | 3 | import ( 4 | "hash" 5 | ) 6 | 7 | // kmac256 represents the partial evaluation of a checksum. 8 | type kmac256 struct { 9 | h hash.Hash 10 | ik [32]byte 11 | len uint64 12 | } 13 | 14 | // NewKmac256 returns a new hash.Hash computing the Kmac256 checksum 15 | func NewKmac256(key []byte) (hash.Hash, error) { 16 | l := len(key) 17 | if l != 32 { 18 | return nil, KeySizeError(l) 19 | } 20 | 21 | d := new(kmac256) 22 | d.h = New256() 23 | d.init(key) 24 | 25 | return d, nil 26 | } 27 | 28 | func (d *kmac256) init(key []byte) { 29 | d.h.Write(key) 30 | d.h.Write(kpad32[:]) 31 | 32 | d.len = 0 33 | for i := 0; i < 32; i++ { 34 | d.ik[i] = ^key[i] 35 | } 36 | } 37 | 38 | func (d *kmac256) Reset() { 39 | d.h.Reset() 40 | } 41 | 42 | func (d *kmac256) Size() int { 43 | return d.h.Size() 44 | } 45 | 46 | func (d *kmac256) BlockSize() int { 47 | return d.h.BlockSize() 48 | } 49 | 50 | func (d *kmac256) Write(p []byte) (nn int, err error) { 51 | d.len += uint64(len(p)) 52 | return d.h.Write(p) 53 | } 54 | 55 | func (d *kmac256) Sum(in []byte) []byte { 56 | // Make a copy of d so that caller can keep writing and summing. 57 | d0 := *d 58 | hash := d0.checkSum() 59 | return append(in, hash...) 60 | } 61 | 62 | func (d *kmac256) checkSum() []byte { 63 | var n uint64 = d.len 64 | var pad_size uint64 65 | 66 | if n < 52 { 67 | pad_size = 51 - n 68 | } else { 69 | pad_size = 63 - ((n - 52) % 64) 70 | } 71 | 72 | n = n * 8 73 | 74 | d.h.Write(dpad[:pad_size + 1]) 75 | 76 | nbytes := uint64sToBytes([]uint64{n}) 77 | d.h.Write(nbytes) 78 | 79 | d.h.Write(dpad[16:20]) 80 | d.h.Write(d.ik[:]) 81 | 82 | hash := d.h.Sum(nil) 83 | return hash 84 | } 85 | -------------------------------------------------------------------------------- /kupyna/kmac384.go: -------------------------------------------------------------------------------- 1 | package kupyna 2 | 3 | import ( 4 | "hash" 5 | ) 6 | 7 | // kmac384 represents the partial evaluation of a checksum. 8 | type kmac384 struct { 9 | h hash.Hash 10 | ik [48]byte 11 | len uint64 12 | } 13 | 14 | // NewKmac384 returns a new hash.Hash computing the Kmac384 checksum 15 | func NewKmac384(key []byte) (hash.Hash, error) { 16 | l := len(key) 17 | if l != 48 { 18 | return nil, KeySizeError(l) 19 | } 20 | 21 | d := new(kmac384) 22 | d.h = New384() 23 | d.init(key) 24 | 25 | return d, nil 26 | } 27 | 28 | func (d *kmac384) init(key []byte) { 29 | d.h.Write(key) 30 | d.h.Write(kpad48[:]) 31 | 32 | d.len = 0 33 | for i := 0; i < 48; i++ { 34 | d.ik[i] = ^key[i] 35 | } 36 | } 37 | 38 | func (d *kmac384) Reset() { 39 | d.h.Reset() 40 | } 41 | 42 | func (d *kmac384) Size() int { 43 | return d.h.Size() 44 | } 45 | 46 | func (d *kmac384) BlockSize() int { 47 | return d.h.BlockSize() 48 | } 49 | 50 | func (d *kmac384) Write(p []byte) (nn int, err error) { 51 | d.len += uint64(len(p)) 52 | return d.h.Write(p) 53 | } 54 | 55 | func (d *kmac384) Sum(in []byte) []byte { 56 | // Make a copy of d so that caller can keep writing and summing. 57 | d0 := *d 58 | hash := d0.checkSum() 59 | return append(in, hash...) 60 | } 61 | 62 | func (d *kmac384) checkSum() []byte { 63 | var n uint64 = d.len 64 | var pad_size uint64 65 | 66 | if n < 116 { 67 | pad_size = 115 - n 68 | } else { 69 | pad_size = 127 - ((n - 116) % 128) 70 | } 71 | 72 | n = n * 8 73 | 74 | d.h.Write(dpad[:pad_size + 1]) 75 | 76 | nbytes := uint64sToBytes([]uint64{n}) 77 | d.h.Write(nbytes) 78 | 79 | d.h.Write(dpad[16:20]) 80 | d.h.Write(d.ik[:]) 81 | 82 | hash := d.h.Sum(nil) 83 | return hash 84 | } 85 | -------------------------------------------------------------------------------- /kupyna/kmac512.go: -------------------------------------------------------------------------------- 1 | package kupyna 2 | 3 | import ( 4 | "hash" 5 | ) 6 | 7 | // kmac512 represents the partial evaluation of a checksum. 8 | type kmac512 struct { 9 | h hash.Hash 10 | ik [64]byte 11 | len uint64 12 | } 13 | 14 | // NewKmac512 returns a new hash.Hash computing the Kmac512 checksum 15 | func NewKmac512(key []byte) (hash.Hash, error) { 16 | l := len(key) 17 | if l != 64 { 18 | return nil, KeySizeError(l) 19 | } 20 | 21 | d := new(kmac512) 22 | d.h = New512() 23 | d.init(key) 24 | 25 | return d, nil 26 | } 27 | 28 | func (d *kmac512) init(key []byte) { 29 | d.h.Write(key) 30 | d.h.Write(kpad64[:]) 31 | 32 | d.len = 0 33 | for i := 0; i < 64; i++ { 34 | d.ik[i] = ^key[i] 35 | } 36 | } 37 | 38 | func (d *kmac512) Reset() { 39 | d.h.Reset() 40 | } 41 | 42 | func (d *kmac512) Size() int { 43 | return d.h.Size() 44 | } 45 | 46 | func (d *kmac512) BlockSize() int { 47 | return d.h.BlockSize() 48 | } 49 | 50 | func (d *kmac512) Write(p []byte) (nn int, err error) { 51 | d.len += uint64(len(p)) 52 | return d.h.Write(p) 53 | } 54 | 55 | func (d *kmac512) Sum(in []byte) []byte { 56 | // Make a copy of d so that caller can keep writing and summing. 57 | d0 := *d 58 | hash := d0.checkSum() 59 | return append(in, hash...) 60 | } 61 | 62 | func (d *kmac512) checkSum() []byte { 63 | var n uint64 = d.len 64 | var pad_size uint64 65 | 66 | if n < 116 { 67 | pad_size = 115 - n 68 | } else { 69 | pad_size = 127 - ((n - 116) % 128) 70 | } 71 | 72 | n = n * 8 73 | 74 | d.h.Write(dpad[:pad_size + 1]) 75 | 76 | nbytes := uint64sToBytes([]uint64{n}) 77 | d.h.Write(nbytes) 78 | 79 | d.h.Write(dpad[16:20]) 80 | d.h.Write(d.ik[:]) 81 | 82 | hash := d.h.Sum(nil) 83 | return hash 84 | } 85 | -------------------------------------------------------------------------------- /md2/binary.go: -------------------------------------------------------------------------------- 1 | package md2 2 | 3 | import ( 4 | "errors" 5 | "encoding/binary" 6 | ) 7 | 8 | const ( 9 | chunk = 16 10 | magic = "md2\x03" 11 | marshaledSize = len(magic) + 48 + chunk + 8 + 16 12 | ) 13 | 14 | func (this *digest) MarshalBinary() ([]byte, error) { 15 | b := make([]byte, 0, marshaledSize) 16 | b = append(b, magic...) 17 | 18 | b = append(b, this.s[:]...) 19 | 20 | x := make([]byte, chunk) 21 | copy(x, this.x[:this.nx]) 22 | b = append(b, x...) 23 | 24 | b = appendUint64(b, this.len) 25 | 26 | b = append(b, this.digest[:]...) 27 | 28 | return b, nil 29 | } 30 | 31 | func (this *digest) UnmarshalBinary(b []byte) error { 32 | if len(b) < len(magic) || (string(b[:len(magic)]) != magic) { 33 | return errors.New("go-hash/md2: invalid hash state identifier") 34 | } 35 | 36 | if len(b) != marshaledSize { 37 | return errors.New("go-hash/md2: invalid hash state size") 38 | } 39 | 40 | b = b[len(magic):] 41 | 42 | b = b[copy(this.s[:], b):] 43 | b = b[copy(this.x[:], b):] 44 | 45 | var length uint64 46 | b, length = consumeUint64(b) 47 | 48 | this.nx = int(length % chunk) 49 | this.len = length 50 | 51 | b = b[copy(this.digest[:], b):] 52 | 53 | return nil 54 | } 55 | 56 | func appendUint64(b []byte, x uint64) []byte { 57 | var a [8]byte 58 | binary.BigEndian.PutUint64(a[:], x) 59 | return append(b, a[:]...) 60 | } 61 | 62 | func consumeUint64(b []byte) ([]byte, uint64) { 63 | _ = b[7] 64 | 65 | x := uint64(b[7]) | 66 | uint64(b[6]) << 8 | 67 | uint64(b[5]) << 16 | 68 | uint64(b[4]) << 24 | 69 | uint64(b[3]) << 32 | 70 | uint64(b[2]) << 40 | 71 | uint64(b[1]) << 48 | 72 | uint64(b[0]) << 56 73 | 74 | return b[8:], x 75 | } 76 | -------------------------------------------------------------------------------- /skeins/skeins.go: -------------------------------------------------------------------------------- 1 | package skeins 2 | 3 | import ( 4 | "hash" 5 | ) 6 | 7 | // NewHS256 returns a new hash.Hash computing the Skein-HS224 checksum 8 | func NewHS256(hs int, iv [4]uint64) hash.Hash { 9 | return newDigest256(hs, iv) 10 | } 11 | 12 | // ============== 13 | 14 | // New224 returns a new hash.Hash computing the Skein-224 checksum 15 | func New224() hash.Hash { 16 | return newDigest512(Size224, initVal224) 17 | } 18 | 19 | // New256 returns a new hash.Hash computing the Skein-256 checksum 20 | func New256() hash.Hash { 21 | return newDigest512(Size256, initVal256) 22 | } 23 | 24 | // New384 returns a new hash.Hash computing the Skein-384 checksum 25 | func New384() hash.Hash { 26 | return newDigest512(Size384, initVal384) 27 | } 28 | 29 | // New512 returns a new hash.Hash computing the Skein-512 checksum 30 | func New512() hash.Hash { 31 | return newDigest512(Size512, initVal512) 32 | } 33 | 34 | // Sum224 returns the Skein-224 checksum of the data. 35 | func Sum224(data []byte) (out [Size224]byte) { 36 | h := New224() 37 | h.Write(data) 38 | sum := h.Sum(nil) 39 | 40 | copy(out[:], sum) 41 | return 42 | } 43 | 44 | // Sum256 returns the Skein-256 checksum of the data. 45 | func Sum256(data []byte) (out [Size256]byte) { 46 | h := New256() 47 | h.Write(data) 48 | sum := h.Sum(nil) 49 | 50 | copy(out[:], sum) 51 | return 52 | } 53 | 54 | // Sum384 returns the Skein-384 checksum of the data. 55 | func Sum384(data []byte) (out [Size384]byte) { 56 | h := New384() 57 | h.Write(data) 58 | sum := h.Sum(nil) 59 | 60 | copy(out[:], sum) 61 | return 62 | } 63 | 64 | // Sum512 returns the Skein-512 checksum of the data. 65 | func Sum512(data []byte) (out [Size512]byte) { 66 | h := New512() 67 | h.Write(data) 68 | sum := h.Sum(nil) 69 | 70 | copy(out[:], sum) 71 | return 72 | } 73 | -------------------------------------------------------------------------------- /hash/whirlpool_test.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "fmt" 5 | "bytes" 6 | "testing" 7 | ) 8 | 9 | var whirlpoolTests = []struct { 10 | input string 11 | output string 12 | }{ 13 | {"abcdefghij", "717163de24809ffcf7ff6d5aba72b8d67c2129721953c252a4ddfb107614be857cbd76a9d5927de14633d6bdc9ddf335160b919db5c6f12cb2e6549181912eef"}, 14 | } 15 | 16 | func Test_Whirlpool(t *testing.T) { 17 | assert := assertT(t) 18 | assertError := assertErrorT(t) 19 | 20 | for index, test := range whirlpoolTests { 21 | e := FromString(test.input).Whirlpool() 22 | 23 | t.Run(fmt.Sprintf("whirlpool_test_%d", index), func(t *testing.T) { 24 | assertError(e.Error, "Whirlpool") 25 | assert(test.output, e.ToHexString(), "Whirlpool") 26 | }) 27 | } 28 | } 29 | 30 | func Test_NewWhirlpool(t *testing.T) { 31 | assert := assertT(t) 32 | assertError := assertErrorT(t) 33 | 34 | for index, test := range whirlpoolTests { 35 | e := Hashing().NewWhirlpool().Write([]byte(test.input)).Sum(nil) 36 | 37 | t.Run(fmt.Sprintf("NewWhirlpool_test_%d", index), func(t *testing.T) { 38 | assertError(e.Error, "NewWhirlpool") 39 | assert(test.output, e.ToHexString(), "NewWhirlpool") 40 | }) 41 | } 42 | } 43 | 44 | func Test_NewWhirlpool_WriteReader(t *testing.T) { 45 | assert := assertT(t) 46 | assertError := assertErrorT(t) 47 | 48 | for index, test := range whirlpoolTests { 49 | buf := bytes.NewBuffer([]byte(test.input)) 50 | 51 | e := Hashing().NewWhirlpool().WriteReader(buf).Sum(nil) 52 | 53 | t.Run(fmt.Sprintf("NewWhirlpool_WriteReader_test_%d", index), func(t *testing.T) { 54 | assertError(e.Error, "NewWhirlpool_WriteReader") 55 | assert(test.output, e.ToHexString(), "NewWhirlpool_WriteReader") 56 | }) 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /murmur3/murmur64.go: -------------------------------------------------------------------------------- 1 | package murmur3 2 | 3 | import ( 4 | "hash" 5 | ) 6 | 7 | // digest64 is half a digest128. 8 | type digest64 digest128 9 | 10 | // New64 returns a 64-bit hasher 11 | func New64() hash.Hash64 { 12 | return New64WithSeed(0) 13 | } 14 | 15 | // New64WithSeed returns a 64-bit hasher set with explicit seed value 16 | func New64WithSeed(seed uint32) hash.Hash64 { 17 | d := (*digest64)(New128WithSeed(seed).(*digest128)) 18 | return d 19 | } 20 | 21 | func (d *digest64) Sum(in []byte) []byte { 22 | // Make a copy of d so that caller can keep writing and summing. 23 | d0 := *d 24 | hash := d0.checkSum() 25 | return append(in, hash[:]...) 26 | } 27 | 28 | func (d *digest64) checkSum() []byte { 29 | h1 := d.Sum64() 30 | return append([]byte{}, 31 | byte(h1>>56), byte(h1>>48), byte(h1>>40), byte(h1>>32), 32 | byte(h1>>24), byte(h1>>16), byte(h1>>8), byte(h1)) 33 | } 34 | 35 | func (d *digest64) Sum64() uint64 { 36 | h1, _ := (*digest128)(d).Sum128() 37 | return h1 38 | } 39 | 40 | // Sum64 returns the MurmurHash3 sum of data. It is equivalent to the 41 | // following sequence (without the extra burden and the extra allocation): 42 | // hasher := New64() 43 | // hasher.Write(data) 44 | // return hasher.Sum64() 45 | func Sum64(data []byte) uint64 { 46 | return Sum64WithSeed(data, 0) 47 | } 48 | 49 | // Sum64WithSeed returns the MurmurHash3 sum of data. It is equivalent to the 50 | // following sequence (without the extra burden and the extra allocation): 51 | // hasher := New64WithSeed(seed) 52 | // hasher.Write(data) 53 | // return hasher.Sum64() 54 | func Sum64WithSeed(data []byte, seed uint32) uint64 { 55 | d := &digest128{h1: uint64(seed), h2: uint64(seed)} 56 | d.seed = seed 57 | d.tail = d.bmix(data) 58 | d.clen = len(data) 59 | h1, _ := d.Sum128() 60 | return h1 61 | } 62 | -------------------------------------------------------------------------------- /hash/gost.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "crypto/cipher" 5 | 6 | "github.com/deatil/go-hash/gost/gost341194" 7 | "github.com/deatil/go-hash/gost/gost34112012" 8 | "github.com/deatil/go-hash/gost/gost34112012256" 9 | "github.com/deatil/go-hash/gost/gost34112012512" 10 | ) 11 | 12 | // gost341194 签名 13 | func (this Hash) Gost341194(cipher func([]byte) cipher.Block) Hash { 14 | h := gost341194.New(cipher) 15 | h.Write(this.data) 16 | 17 | this.data = h.Sum(nil) 18 | 19 | return this 20 | } 21 | 22 | // NewGost341194 23 | func (this Hash) NewGost341194(cipher func([]byte) cipher.Block) Hash { 24 | this.hash = gost341194.New(cipher) 25 | 26 | return this 27 | } 28 | 29 | // =============== 30 | 31 | // gost34112012 签名 32 | func (this Hash) Gost34112012(size int) Hash { 33 | h := gost34112012.New(size) 34 | h.Write(this.data) 35 | 36 | this.data = h.Sum(nil) 37 | 38 | return this 39 | } 40 | 41 | // NewGost34112012 42 | func (this Hash) NewGost34112012(size int) Hash { 43 | this.hash = gost34112012.New(size) 44 | 45 | return this 46 | } 47 | 48 | // =============== 49 | 50 | // gost34112012256 签名 51 | func (this Hash) Gost34112012256() Hash { 52 | h := gost34112012256.New() 53 | h.Write(this.data) 54 | 55 | this.data = h.Sum(nil) 56 | 57 | return this 58 | } 59 | 60 | // NewGost34112012256 61 | func (this Hash) NewGost34112012256() Hash { 62 | this.hash = gost34112012256.New() 63 | 64 | return this 65 | } 66 | 67 | // =============== 68 | 69 | // gost34112012512 签名 70 | func (this Hash) Gost34112012512() Hash { 71 | h := gost34112012512.New() 72 | h.Write(this.data) 73 | 74 | this.data = h.Sum(nil) 75 | 76 | return this 77 | } 78 | 79 | // NewGost34112012512 80 | func (this Hash) NewGost34112012512() Hash { 81 | this.hash = gost34112012512.New() 82 | 83 | return this 84 | } 85 | -------------------------------------------------------------------------------- /skein512/skein.go: -------------------------------------------------------------------------------- 1 | // Package skein implements the Skein-512 hash function, MAC, and stream cipher 2 | // as defined in "The Skein Hash Function Family, v1.3". 3 | package skein512 4 | 5 | import ( 6 | "hash" 7 | "crypto/cipher" 8 | ) 9 | 10 | // NewHash returns hash.Hash calculating checksum of the given length in bytes 11 | // (for example, to calculate 256-bit hash, outLen must be set to 32). 12 | func NewHash(outLen uint64) hash.Hash { 13 | return hash.Hash(New(outLen, nil)) 14 | } 15 | 16 | // NewHash224 returns a new hash.Hash computing the Skein-224 checksum 17 | func NewHash224() hash.Hash { 18 | return NewHash(28) 19 | } 20 | 21 | // NewHash256 returns a new hash.Hash computing the Skein-256 checksum 22 | func NewHash256() hash.Hash { 23 | return NewHash(32) 24 | } 25 | 26 | // NewHash384 returns a new hash.Hash computing the Skein-384 checksum 27 | func NewHash384() hash.Hash { 28 | return NewHash(48) 29 | } 30 | 31 | // NewHash512 returns a new hash.Hash computing the Skein-512 checksum 32 | func NewHash512() hash.Hash { 33 | return NewHash(64) 34 | } 35 | 36 | // NewMAC returns hash.Hash calculating Skein Message Authentication Code of the 37 | // given length in bytes. A MAC is a cryptographic hash that uses a key to 38 | // authenticate a message. The receiver verifies the hash by recomputing it 39 | // using the same key. 40 | func NewMAC(outLen uint64, key []byte) hash.Hash { 41 | return hash.Hash(New(outLen, &Args{Key: key})) 42 | } 43 | 44 | // NewStream returns a cipher.Stream for encrypting a message with the given key 45 | // and nonce. The same key-nonce combination must not be used to encrypt more 46 | // than one message. There are no limits on the length of key or nonce. 47 | func NewStream(key []byte, nonce []byte) cipher.Stream { 48 | const streamOutLen = (1<<64 - 1) / 8 // 2^64 - 1 bits 49 | h := New(streamOutLen, &Args{Key: key, Nonce: nonce, NoMsg: true}) 50 | return newOutputReader(h) 51 | } 52 | -------------------------------------------------------------------------------- /hash/jh_test.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | var jhTests = []struct { 9 | output string 10 | input string 11 | }{ 12 | {"ce05383a3f918867994e9288d40adc5b735ff1a4f7a7f8cb9c50f0dc72328b66", "abcd"}, 13 | {"40755d6a2482e7b66e0abbbcedb3ace7d22414e468c1390810ad991cd707aeff", "abcdabcdabcdab"}, 14 | {"e9a0d9a0328df56e4d5634e825133e625892c582a2bb8dd8c963c37399083bd0", "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd"}, 15 | {"3c4b76ca6be2d3c9d9e0af207557301cd9bbf9f478e1dee7f3303ad7eca24249", "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabc"}, 16 | {"f083c34d561a5fff98a8ef1fbafb58ed5d1fe51e88545bacfcbf5b698da5206b", "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabc"}, 17 | } 18 | 19 | func Test_JH(t *testing.T) { 20 | assert := assertT(t) 21 | assertError := assertErrorT(t) 22 | 23 | for index, test := range jhTests { 24 | e := FromString(test.input).JH() 25 | 26 | t.Run(fmt.Sprintf("JH %d", index), func(t *testing.T) { 27 | assertError(e.Error, "error") 28 | assert(e.ToHexString(), test.output, "to hex") 29 | }) 30 | } 31 | } 32 | 33 | func Test_NewJH(t *testing.T) { 34 | assert := assertT(t) 35 | assertError := assertErrorT(t) 36 | 37 | for index, test := range jhTests { 38 | e := FromString("").NewJH().Write([]byte(test.input)).Sum(nil) 39 | 40 | t.Run(fmt.Sprintf("NewJH %d", index), func(t *testing.T) { 41 | assertError(e.Error, "error") 42 | assert(e.ToHexString(), test.output, "to hex") 43 | }) 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /blake3/output.go: -------------------------------------------------------------------------------- 1 | package blake3 2 | 3 | type output struct { 4 | input_cv [8]uint32 5 | counter uint64 6 | block [BLAKE3_BLOCK_LEN]byte 7 | block_len uint8 8 | flags uint8 9 | } 10 | 11 | func newOutput( 12 | input_cv [8]uint32, 13 | block [BLAKE3_BLOCK_LEN]byte, 14 | block_len uint8, 15 | counter uint64, 16 | flags uint8, 17 | ) output { 18 | var ret output 19 | copy(ret.input_cv[:], input_cv[:]) 20 | copy(ret.block[:], block[:]) 21 | 22 | ret.block_len = block_len 23 | ret.counter = counter 24 | ret.flags = flags 25 | 26 | return ret 27 | } 28 | 29 | func (o *output) chainingValue(cv *[32]byte) { 30 | var cv_words [8]uint32 31 | copy(cv_words[:], o.input_cv[:]) 32 | 33 | blake3_compress_in_place( 34 | &cv_words, 35 | o.block, 36 | o.block_len, 37 | o.counter, 38 | o.flags, 39 | ) 40 | 41 | buf := uint32sToBytes(cv_words[:]) 42 | copy(cv[:], buf) 43 | } 44 | 45 | func (o *output) rootBytes(seek uint64, out []byte, out_len int) { 46 | var output_block_counter uint64 = seek / 64; 47 | var offset_within_block int = int(seek % 64) 48 | var wide_buf [64]byte 49 | 50 | for out_len > 0 { 51 | blake3_compress_xof( 52 | o.input_cv, 53 | o.block, 54 | o.block_len, 55 | output_block_counter, 56 | o.flags | ROOT, 57 | &wide_buf, 58 | ) 59 | 60 | var available_bytes int = 64 - offset_within_block 61 | var memcpy_len int 62 | 63 | if out_len > available_bytes { 64 | memcpy_len = available_bytes 65 | } else { 66 | memcpy_len = out_len 67 | } 68 | 69 | copy(out[:], wide_buf[offset_within_block:memcpy_len]) 70 | 71 | out = out[memcpy_len:] 72 | out_len -= memcpy_len 73 | 74 | output_block_counter += 1 75 | offset_within_block = 0 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /hash/hash_data_set.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "sync" 5 | ) 6 | 7 | type DataName interface { 8 | ~uint | ~int | ~string 9 | } 10 | 11 | /** 12 | * 数据设置 13 | * 14 | * @create 2023-3-31 15 | * @author deatil 16 | */ 17 | type DataSet[N DataName, M any] struct { 18 | // 锁定 19 | mu sync.RWMutex 20 | 21 | // 数据 22 | data map[N]func() M 23 | } 24 | 25 | // 构造函数 26 | func NewDataSet[N DataName, M any]() *DataSet[N, M] { 27 | return &DataSet[N, M]{ 28 | data: make(map[N]func() M), 29 | } 30 | } 31 | 32 | // 设置 33 | func (this *DataSet[N, M]) Add(name N, data func() M) *DataSet[N, M] { 34 | this.mu.Lock() 35 | defer this.mu.Unlock() 36 | 37 | this.data[name] = data 38 | 39 | return this 40 | } 41 | 42 | func (this *DataSet[N, M]) Has(name N) bool { 43 | this.mu.RLock() 44 | defer this.mu.RUnlock() 45 | 46 | if _, ok := this.data[name]; ok { 47 | return true 48 | } 49 | 50 | return false 51 | } 52 | 53 | func (this *DataSet[N, M]) Get(name N) func() M { 54 | this.mu.RLock() 55 | defer this.mu.RUnlock() 56 | 57 | if data, ok := this.data[name]; ok { 58 | return data 59 | } 60 | 61 | return nil 62 | } 63 | 64 | // 删除 65 | func (this *DataSet[N, M]) Remove(name N) *DataSet[N, M] { 66 | this.mu.Lock() 67 | defer this.mu.Unlock() 68 | 69 | delete(this.data, name) 70 | 71 | return this 72 | } 73 | 74 | func (this *DataSet[N, M]) Names() []N { 75 | names := make([]N, 0) 76 | for name, _ := range this.data { 77 | names = append(names, name) 78 | } 79 | 80 | return names 81 | } 82 | 83 | func (this *DataSet[N, M]) All() map[N]func() M { 84 | return this.data 85 | } 86 | 87 | func (this *DataSet[N, M]) Clean() { 88 | this.mu.Lock() 89 | defer this.mu.Unlock() 90 | 91 | for name, _ := range this.data { 92 | delete(this.data, name) 93 | } 94 | } 95 | 96 | func (this *DataSet[N, M]) Len() int { 97 | return len(this.data) 98 | } 99 | -------------------------------------------------------------------------------- /lsh512/lsh512_test.go: -------------------------------------------------------------------------------- 1 | package lsh512 2 | 3 | import ( 4 | "bytes" 5 | "testing" 6 | "encoding/hex" 7 | ) 8 | 9 | func fromHex(s string) []byte { 10 | h, _ := hex.DecodeString(s) 11 | return h 12 | } 13 | 14 | func Test_Size224(t *testing.T) { 15 | msg := fromHex("57F3F5DA1A7B291FEB6B7044D1") 16 | md := fromHex("24D62573B16EEB4938A00FB0CD5319CD3A2A864E5DAB50322D106535") 17 | 18 | h := New224() 19 | h.Reset() 20 | h.Write(msg) 21 | dst := h.Sum(nil) 22 | 23 | if !bytes.Equal(dst, md) { 24 | t.Errorf("hash failed. got %x, want %x", dst, md) 25 | return 26 | } 27 | } 28 | 29 | func Test_Size256(t *testing.T) { 30 | msg := fromHex("7D3272CAEEAEDB3CD291BD2D719A31683C04D327") 31 | md := fromHex("CC776D0F55C46F482F4CAEA9CA72C5C74AEE77342F9FF6B62052CB445626B623") 32 | 33 | h := New256() 34 | h.Reset() 35 | h.Write(msg) 36 | dst := h.Sum(nil) 37 | 38 | if !bytes.Equal(dst, md) { 39 | t.Errorf("hash failed. got %x, want %x", dst, md) 40 | return 41 | } 42 | } 43 | 44 | func Test_Size384(t *testing.T) { 45 | msg := fromHex("75B6078992FAA7A0A4FD3476EC0C98DF3BC0D55D5AD19197AA") 46 | md := fromHex("735B0F1C984E6F8E03B84720AE7C66E537CE9E451F04A6826435948F491FA0F1C906FA55A7A0CD7A734F51BC1F0536C1") 47 | 48 | h := New384() 49 | h.Reset() 50 | h.Write(msg) 51 | dst := h.Sum(nil) 52 | 53 | if !bytes.Equal(dst, md) { 54 | t.Errorf("hash failed. got %x, want %x", dst, md) 55 | return 56 | } 57 | } 58 | 59 | func Test_Size512(t *testing.T) { 60 | msg := fromHex("78AECC1F4DBF27AC146780EEA8DCC56B858163329665B677480CC47D") 61 | md := fromHex("1074CFC160290EF5B9E98BA729817FE58BFE3CB699CAFE2AABAF28759E2D82869F148104330C8F02BE2A4BCB90E9C9630E9CC5685250E8115EC06323B1E21C54") 62 | 63 | h := New() 64 | h.Reset() 65 | h.Write(msg) 66 | dst := h.Sum(nil) 67 | 68 | if !bytes.Equal(dst, md) { 69 | t.Errorf("hash failed. got %x, want %x", dst, md) 70 | return 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /lsh512/lsh512.go: -------------------------------------------------------------------------------- 1 | package lsh512 2 | 3 | import ( 4 | "hash" 5 | ) 6 | 7 | const ( 8 | // The size of a LSH-512 checksum in bytes. 9 | Size = 64 10 | // The size of a LSH-512-224 checksum in bytes. 11 | Size224 = 28 12 | // The size of a LSH-512-256 checksum in bytes. 13 | Size256 = 32 14 | // The size of a LSH-384 checksum in bytes. 15 | Size384 = 48 16 | 17 | // The blocksize of LSH-512, LSH-384, LSH-512-256 and LSH-512-224 in bytes. 18 | BlockSize = 256 19 | ) 20 | 21 | // New returns a new hash.Hash computing the LSH-512 checksum. 22 | func New() hash.Hash { 23 | return newDigest(Size) 24 | } 25 | 26 | // New384 returns a new hash.Hash computing the LSH-384 checksum. 27 | func New384() hash.Hash { 28 | return newDigest(Size384) 29 | } 30 | 31 | // New256 returns a new hash.Hash computing the LSH-512-256 checksum. 32 | func New256() hash.Hash { 33 | return newDigest(Size256) 34 | } 35 | 36 | // New224 returns a new hash.Hash computing the LSH-512-224 checksum. 37 | func New224() hash.Hash { 38 | return newDigest(Size224) 39 | } 40 | 41 | // Sum returns the LSH-512 checksum of the data. 42 | func Sum(data []byte) (sum512 [Size]byte) { 43 | b := New() 44 | b.Write(data) 45 | sum := b.Sum(nil) 46 | 47 | copy(sum512[:], sum) 48 | return 49 | } 50 | 51 | // Sum384 returns the LSH-384 checksum of the data. 52 | func Sum384(data []byte) (sum384 [Size384]byte) { 53 | b := New() 54 | b.Write(data) 55 | sum := b.Sum(nil) 56 | 57 | copy(sum384[:], sum) 58 | return 59 | } 60 | 61 | // Sum256 returns the LSH-512-256 checksum of the data. 62 | func Sum256(data []byte) (sum256 [Size256]byte) { 63 | b := New() 64 | b.Write(data) 65 | sum := b.Sum(nil) 66 | 67 | copy(sum256[:], sum) 68 | return 69 | } 70 | 71 | // Sum224 returns the LSH-512-224 checksum of the data. 72 | func Sum224(data []byte) (sum224 [Size224]byte) { 73 | b := New() 74 | b.Write(data) 75 | sum := b.Sum(nil) 76 | 77 | copy(sum224[:], sum) 78 | return 79 | } 80 | -------------------------------------------------------------------------------- /hash/blake.go: -------------------------------------------------------------------------------- 1 | package hash 2 | 3 | import ( 4 | "golang.org/x/crypto/blake2b" 5 | "golang.org/x/crypto/blake2s" 6 | ) 7 | 8 | // Blake2b_256 哈希值 9 | func (this Hash) Blake2b_256() Hash { 10 | sum := blake2b.Sum256(this.data) 11 | this.data = sum[:] 12 | 13 | return this 14 | } 15 | 16 | // NewBlake2b_256 17 | func (this Hash) NewBlake2b_256(key []byte) Hash { 18 | this.hash, this.Error = blake2b.New256(key) 19 | 20 | return this 21 | } 22 | 23 | // =========== 24 | 25 | // Blake2b_384 哈希值 26 | func (this Hash) Blake2b_384() Hash { 27 | sum := blake2b.Sum384(this.data) 28 | this.data = sum[:] 29 | 30 | return this 31 | } 32 | 33 | // NewBlake2b_384 34 | func (this Hash) NewBlake2b_384(key []byte) Hash { 35 | this.hash, this.Error = blake2b.New384(key) 36 | 37 | return this 38 | } 39 | 40 | // =========== 41 | 42 | // Blake2b_512 哈希值 43 | func (this Hash) Blake2b_512() Hash { 44 | sum := blake2b.Sum512(this.data) 45 | this.data = sum[:] 46 | 47 | return this 48 | } 49 | 50 | // NewBlake2b_512 51 | func (this Hash) NewBlake2b_512(key []byte) Hash { 52 | this.hash, this.Error = blake2b.New512(key) 53 | 54 | return this 55 | } 56 | 57 | // =========== 58 | 59 | // Blake2s_256 哈希值 60 | func (this Hash) Blake2s_256() Hash { 61 | sum := blake2s.Sum256(this.data) 62 | this.data = sum[:] 63 | 64 | return this 65 | } 66 | 67 | // NewBlake2s_256 68 | func (this Hash) NewBlake2s_256(key []byte) Hash { 69 | this.hash, this.Error = blake2s.New256(key) 70 | 71 | return this 72 | } 73 | 74 | // =========== 75 | 76 | // Blake2s_128 哈希值 77 | func (this Hash) Blake2s_128() Hash { 78 | h, err := blake2s.New128(nil) 79 | if err != nil { 80 | this.Error = err 81 | return this 82 | } 83 | 84 | h.Write(this.data) 85 | 86 | this.data = h.Sum(nil) 87 | 88 | return this 89 | } 90 | 91 | // NewBlake2s_128 92 | func (this Hash) NewBlake2s_128(key []byte) Hash { 93 | this.hash, this.Error = blake2s.New128(key) 94 | 95 | return this 96 | } 97 | --------------------------------------------------------------------------------