go.temporal.io/server@v1.23.0/common/checksum/crc.go (about) 1 // The MIT License 2 // 3 // Copyright (c) 2020 Temporal Technologies Inc. All rights reserved. 4 // 5 // Copyright (c) 2020 Uber Technologies, Inc. 6 // 7 // Permission is hereby granted, free of charge, to any person obtaining a copy 8 // of this software and associated documentation files (the "Software"), to deal 9 // in the Software without restriction, including without limitation the rights 10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 // copies of the Software, and to permit persons to whom the Software is 12 // furnished to do so, subject to the following conditions: 13 // 14 // The above copyright notice and this permission notice shall be included in 15 // all copies or substantial portions of the Software. 16 // 17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 // THE SOFTWARE. 24 25 package checksum 26 27 import ( 28 "bytes" 29 "encoding/binary" 30 "errors" 31 "fmt" 32 "hash/crc32" 33 34 enumsspb "go.temporal.io/server/api/enums/v1" 35 persistencespb "go.temporal.io/server/api/persistence/v1" 36 ) 37 38 // ErrMismatch indicates a checksum verification failure due to 39 // a derived checksum not being equal to expected checksum 40 var ErrMismatch = errors.New("checksum mismatch error") 41 42 type Marshaler interface { 43 Marshal() ([]byte, error) 44 } 45 46 // GenerateCRC32 generates an IEEE crc32 checksum on the 47 // serilized byte array of the given thrift object. The 48 // serialization proto used will be of type thriftRW 49 func GenerateCRC32( 50 payload Marshaler, 51 payloadVersion int32, 52 ) (*persistencespb.Checksum, error) { 53 54 payloadBytes, err := payload.Marshal() 55 if err != nil { 56 return nil, err 57 } 58 59 crc := crc32.ChecksumIEEE(payloadBytes) 60 checksum := make([]byte, 4) 61 binary.BigEndian.PutUint32(checksum, crc) 62 return &persistencespb.Checksum{ 63 Value: checksum, 64 Version: payloadVersion, 65 Flavor: enumsspb.CHECKSUM_FLAVOR_IEEE_CRC32_OVER_PROTO3_BINARY, 66 }, nil 67 } 68 69 // Verify verifies that the checksum generated from the 70 // given thrift object matches the specified expected checksum 71 // Return ErrMismatch when checksums mismatch 72 func Verify( 73 payload Marshaler, 74 checksum *persistencespb.Checksum, 75 ) error { 76 77 if checksum.Flavor != enumsspb.CHECKSUM_FLAVOR_IEEE_CRC32_OVER_PROTO3_BINARY { 78 return fmt.Errorf("unknown checksum flavor %v", checksum.Flavor) 79 } 80 81 expected, err := GenerateCRC32(payload, checksum.Version) 82 if err != nil { 83 return err 84 } 85 86 if !bytes.Equal(expected.GetValue(), checksum.GetValue()) { 87 return ErrMismatch 88 } 89 90 return nil 91 }