OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package internal | |
6 | |
7 import ( | |
8 "bytes" | |
9 "crypto/sha1" | |
10 "errors" | |
11 | |
12 "github.com/golang/protobuf/proto" | |
13 | |
14 "infra/tools/cipd/internal/messages" | |
15 ) | |
16 | |
17 // MarshalWithSHA1 serializes proto message to bytes, calculates SHA1 checksum | |
18 // of it, and returns serialized envelope that contains both. UnmarshalWithSHA1 | |
19 // can then be used to verify SHA1 and deserialized the original object. | |
20 func MarshalWithSHA1(pm proto.Message) ([]byte, error) { | |
21 blob, err := proto.Marshal(pm) | |
22 if err != nil { | |
23 return nil, err | |
24 } | |
25 sum := sha1.Sum(blob) | |
26 envelope := messages.BlobWithSHA1{Blob: blob, Sha1: sum[:]} | |
27 return proto.Marshal(&envelope) | |
28 } | |
29 | |
30 // UnmarshalWithSHA1Check is reverse of MarshalWithSHA1. It checks SHA1 checksum | |
31 // and deserializes the object if it matches the blob. | |
32 func UnmarshalWithSHA1(buf []byte, pm proto.Message) error { | |
33 envelope := messages.BlobWithSHA1{} | |
nodir
2015/09/30 19:08:23
I prefer
var envelope messages.BlobWithSHA1
b
Vadim Sh.
2015/09/30 22:50:59
And I prefer var := type{} :)
| |
34 if err := proto.Unmarshal(buf, &envelope); err != nil { | |
35 return err | |
36 } | |
37 sum := sha1.Sum(envelope.GetBlob()) | |
38 if !bytes.Equal(sum[:], envelope.GetSha1()) { | |
39 return errors.New("check sum of tag cache file is invalid") | |
40 } | |
41 return proto.Unmarshal(envelope.GetBlob(), pm) | |
42 } | |
OLD | NEW |