Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(746)

Side by Side Diff: go/src/infra/tools/cipd/internal/checksum.go

Issue 1381583007: cipd: Implement local cache for ResolveVersion(...) call with tags. (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@cipd-init
Patch Set: rebase Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « go/src/infra/tools/cipd/client.go ('k') | go/src/infra/tools/cipd/internal/checksum_test.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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{}
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 }
OLDNEW
« no previous file with comments | « go/src/infra/tools/cipd/client.go ('k') | go/src/infra/tools/cipd/internal/checksum_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698