/
hash.go
36 lines (31 loc) · 1.13 KB
/
hash.go
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
package types
import (
"crypto/rand"
"encoding/binary"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/tendermint/tendermint/crypto/tmhash"
)
// GenerateSecureRandomNumber generates cryptographically strong pseudo-random number
func GenerateSecureRandomNumber() ([]byte, error) {
bytes := make([]byte, 32)
if _, err := rand.Read(bytes); err != nil {
return []byte{}, err
}
return bytes, nil
}
// CalculateRandomHash calculates the hash of a number and timestamp
func CalculateRandomHash(randomNumber []byte, timestamp int64) []byte {
data := make([]byte, RandomNumberLength+Int64Size)
copy(data[:RandomNumberLength], randomNumber)
binary.BigEndian.PutUint64(data[RandomNumberLength:], uint64(timestamp))
return tmhash.Sum(data)
}
// CalculateSwapID calculates the hash of a RandomNumberHash, sdk.AccAddress, and string
func CalculateSwapID(randomNumberHash []byte, sender sdk.AccAddress, senderOtherChain string) []byte {
senderOtherChain = strings.ToLower(senderOtherChain)
data := randomNumberHash
data = append(data, sender.Bytes()...)
data = append(data, []byte(senderOtherChain)...)
return tmhash.Sum(data)
}