hash.go (2825B)
1 // Copyright 2026 Shivakumar (Certichain) 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package crypto 16 17 import ( 18 "crypto/sha256" 19 "encoding/binary" 20 "encoding/hex" 21 "encoding/json" 22 "errors" 23 "fmt" 24 ) 25 26 const ( 27 MaxFieldSize = 16 * 1024 * 1024 // 16 MB 28 lengthPrefixSize = 8 // 8 bytes for uint64 29 HashSize = 32 // sha256 digest 30 ) 31 32 type Hash [HashSize]byte 33 34 // CanonicalBytes function encodes the fields into a deterministic 35 // byte sequence: 36 // 37 // |[len(field_1)][field_1][len(field_2)][field_2]...| 38 // 39 // Each length is encoded as an uint64 BigEndian. 40 func CanonicalBytes(fields ...[]byte) ([]byte, error) { 41 bufSize := 0 42 for _, field := range fields { 43 if len(field) > MaxFieldSize { 44 return nil, errors.New("canonicalbytes: field too large") 45 } 46 bufSize += lengthPrefixSize + len(field) 47 } 48 49 byteArr := make([]byte, bufSize) 50 offset := 0 51 for _, field := range fields { 52 binary.BigEndian.PutUint64( 53 byteArr[offset:offset+8], 54 uint64(len(field)), 55 ) 56 57 offset += lengthPrefixSize 58 59 copy(byteArr[offset:offset+len(field)], field) 60 61 offset += len(field) 62 } 63 64 return byteArr, nil 65 } 66 67 // HashBytes computes the SHA-256 hash of the data. 68 func HashBytes(data []byte) Hash { 69 return sha256.Sum256(data) 70 } 71 72 // HashCanonical function canonicalizes the fields and hashes them 73 // to get a SHA-256 32 byte digest. 74 func HashCanonical(fields ...[]byte) (Hash, error) { 75 canonical, err := CanonicalBytes(fields...) 76 if err != nil { 77 return Hash{}, err 78 } 79 80 return HashBytes(canonical), nil 81 } 82 83 // IsZero checks if the hash is empty. 84 func (h Hash) IsZero() bool { 85 return h == Hash{} 86 } 87 88 // String returns the hex representation of the hash. 89 func (h Hash) String() string { 90 return hex.EncodeToString(h[:]) 91 } 92 93 // MarshalJSON encodes the hash as a hex string for JSON. 94 func (h Hash) MarshalJSON() ([]byte, error) { 95 return json.Marshal(h.String()) 96 } 97 98 // UnmarshalJSON decodes a hex string into a Hash. 99 func (h *Hash) UnmarshalJSON(data []byte) error { 100 var s string 101 if err := json.Unmarshal(data, &s); err != nil { 102 return err 103 } 104 105 decoded, err := hex.DecodeString(s) 106 if err != nil { 107 return err 108 } 109 if len(decoded) != HashSize { 110 return fmt.Errorf("crypto: invalid hash length: expected = %d, got = %d", HashSize, len(decoded)) 111 } 112 copy(h[:], decoded) 113 114 return nil 115 }