-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash.go
More file actions
42 lines (35 loc) · 1.11 KB
/
hash.go
File metadata and controls
42 lines (35 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package outscript
import (
"crypto/sha256"
"fmt"
"hash"
"github.com/BottleFmt/gobottle"
"golang.org/x/crypto/ripemd160"
)
// IHashInfo is an [Insertable] that hashes the output of another [Insertable] using one
// or more chained hash functions.
type IHashInfo struct {
v Insertable
hash []func() hash.Hash
}
// Bytes hashes the output of the wrapped Insertable using the configured hash chain.
func (i IHashInfo) Bytes(s *Script) ([]byte, error) {
v, err := i.v.Bytes(s)
if err != nil {
return nil, err
}
return gobottle.Hash(v, i.hash...), nil
}
// String returns a human-readable representation of the hash operation.
func (i IHashInfo) String() string {
return fmt.Sprintf("Hash(%s, %v)", i.v, i.hash)
}
// IHash returns an [IHashInfo] that hashes the output of v using the given hash functions in sequence.
func IHash(v Insertable, hash ...func() hash.Hash) IHashInfo {
return IHashInfo{v: v, hash: hash}
}
// IHash160 returns an [IHashInfo] that computes HASH160 (SHA-256 followed by RIPEMD-160) of the
// output of v.
func IHash160(v Insertable) IHashInfo {
return IHash(v, sha256.New, ripemd160.New)
}