github.com/turingchain2020/turingchain@v1.1.21/common/hash.go (about) 1 // Copyright Turing Corp. 2018 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 common 6 7 import ( 8 "crypto/sha256" 9 "encoding/hex" 10 11 "github.com/turingchain2020/turingchain/common/crypto/sha3" 12 "golang.org/x/crypto/ripemd160" 13 ) 14 15 //Sha256Len sha256 bytes len 16 const Sha256Len = 32 17 18 //Hash type 19 type Hash [Sha256Len]byte 20 21 //BytesToHash []byte -> hash 22 func BytesToHash(b []byte) Hash { 23 var h Hash 24 h.SetBytes(b) 25 return h 26 } 27 28 //HexToHash hex -> hash 29 func HexToHash(s string) Hash { 30 b, err := FromHex(s) 31 if err != nil { 32 panic(err) 33 } 34 return BytesToHash(b) 35 } 36 37 //Bytes Get the []byte representation of the underlying hash 38 func (h Hash) Bytes() []byte { return h[:] } 39 40 //SetBytes Sets the hash to the value of b. If b is larger than len(h), 'b' will be cropped (from the left). 41 func (h *Hash) SetBytes(b []byte) { 42 if len(b) > len(h) { 43 b = b[len(b)-Sha256Len:] 44 } 45 copy(h[Sha256Len-len(b):], b) 46 } 47 48 //ToHex []byte -> hex 49 func ToHex(b []byte) string { 50 hex := hex.EncodeToString(b) 51 // Prefer output of "0x0" instead of "0x" 52 if len(hex) == 0 { 53 return "" 54 } 55 return "0x" + hex 56 } 57 58 //HashHex []byte -> hex 59 func HashHex(d []byte) string { 60 var buf [Sha256Len * 2]byte 61 hex.Encode(buf[:], d) 62 return string(buf[:]) 63 } 64 65 //FromHex hex -> []byte 66 func FromHex(s string) ([]byte, error) { 67 if len(s) > 1 { 68 if s[0:2] == "0x" || s[0:2] == "0X" { 69 s = s[2:] 70 } 71 if len(s)%2 == 1 { 72 s = "0" + s 73 } 74 return hex.DecodeString(s) 75 } 76 return []byte{}, nil 77 } 78 79 // CopyBytes Returns an exact copy of the provided bytes 80 func CopyBytes(b []byte) (copiedBytes []byte) { 81 if b == nil { 82 return nil 83 } 84 copiedBytes = make([]byte, len(b)) 85 copy(copiedBytes, b) 86 87 return 88 } 89 90 //IsHex 是否是hex字符串 91 func IsHex(str string) bool { 92 l := len(str) 93 return l >= 4 && l%2 == 0 && str[0:2] == "0x" 94 } 95 96 //Sha256 加密 97 func Sha256(b []byte) []byte { 98 data := sha256.Sum256(b) 99 return data[:] 100 } 101 102 //Sha3 加密 103 func Sha3(b []byte) []byte { 104 data := sha3.KeccakSum256(b) 105 return data[:] 106 } 107 108 // Sha2Sum Returns hash: SHA256( SHA256( data ) ) 109 // Where possible, using ShaHash() should be a bit faster 110 func Sha2Sum(b []byte) []byte { 111 tmp := sha256.Sum256(b) 112 tmp = sha256.Sum256(tmp[:]) 113 return tmp[:] 114 } 115 116 func rimpHash(in []byte, out []byte) { 117 sha := sha256.New() 118 _, err := sha.Write(in) 119 if err != nil { 120 return 121 } 122 rim := ripemd160.New() 123 _, err = rim.Write(sha.Sum(nil)[:]) 124 if err != nil { 125 return 126 } 127 copy(out, rim.Sum(nil)) 128 } 129 130 // Rimp160 Returns hash: RIMP160( SHA256( data ) ) 131 // Where possible, using RimpHash() should be a bit faster 132 func Rimp160(b []byte) []byte { 133 out := make([]byte, 20) 134 rimpHash(b, out[:]) 135 return out[:] 136 }