mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-12-25 13:56:35 +00:00
51 lines
1.1 KiB
Go
51 lines
1.1 KiB
Go
package hash
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
"golang.org/x/crypto/ripemd160"
|
|
)
|
|
|
|
// Sha256 hashes the incoming byte slice
|
|
// using the sha256 algorithm.
|
|
func Sha256(data []byte) util.Uint256 {
|
|
hash := sha256.Sum256(data)
|
|
return hash
|
|
}
|
|
|
|
// DoubleSha256 performs sha256 twice on the given data.
|
|
func DoubleSha256(data []byte) util.Uint256 {
|
|
var hash util.Uint256
|
|
|
|
h1 := Sha256(data)
|
|
hash = Sha256(h1.BytesBE())
|
|
return hash
|
|
}
|
|
|
|
// RipeMD160 performs the RIPEMD160 hash algorithm
|
|
// on the given data.
|
|
func RipeMD160(data []byte) util.Uint160 {
|
|
var hash util.Uint160
|
|
hasher := ripemd160.New()
|
|
_, _ = hasher.Write(data)
|
|
|
|
hash, _ = util.Uint160DecodeBytesBE(hasher.Sum(nil))
|
|
return hash
|
|
}
|
|
|
|
// Hash160 performs sha256 and then ripemd160
|
|
// on the given data.
|
|
func Hash160(data []byte) util.Uint160 {
|
|
h1 := Sha256(data)
|
|
h2 := RipeMD160(h1.BytesBE())
|
|
|
|
return h2
|
|
}
|
|
|
|
// Checksum returns the checksum for a given piece of data
|
|
// using sha256 twice as the hash algorithm.
|
|
func Checksum(data []byte) []byte {
|
|
hash := DoubleSha256(data)
|
|
return hash[:4]
|
|
}
|