Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The LUCI Authors. All rights reserved. | |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | |
| 3 // that can be found in the LICENSE file. | |
| 4 | |
| 5 package caching | |
| 6 | |
| 7 import ( | |
| 8 "bytes" | |
| 9 "compress/zlib" | |
| 10 "crypto/sha256" | |
| 11 "encoding/binary" | |
| 12 "encoding/json" | |
| 13 "strings" | |
| 14 | |
| 15 "github.com/luci/luci-go/common/errors" | |
| 16 ) | |
| 17 | |
| 18 // Encode is a convenience method for generating a ZLIB-compressed JSON-encoded | |
| 19 // object. | |
| 20 func Encode(v interface{}) ([]byte, error) { | |
|
iannucci
2017/01/07 20:53:16
maybe EncodeJSONZlib / DecodeJSONZlib?
dnj
2017/01/10 03:29:17
i was thinking I'd leave it ambiguous in function
| |
| 21 var buf bytes.Buffer | |
| 22 zw := zlib.NewWriter(&buf) | |
| 23 enc := json.NewEncoder(zw) | |
| 24 | |
| 25 if err := enc.Encode(v); err != nil { | |
| 26 zw.Close() | |
| 27 return nil, errors.Annotate(err).Reason("failed to JSON-encode") .Err() | |
| 28 } | |
| 29 if err := zw.Close(); err != nil { | |
| 30 return nil, errors.Annotate(err).Reason("failed to Close zlib Wr iter").Err() | |
| 31 } | |
| 32 return buf.Bytes(), nil | |
| 33 } | |
| 34 | |
| 35 // Decode is a convenience method for decoding a ZLIB-compressed JSON-encoded | |
| 36 // object encoded by Encode. | |
| 37 func Decode(d []byte, v interface{}) error { | |
| 38 zr, err := zlib.NewReader(bytes.NewReader(d)) | |
| 39 if err != nil { | |
| 40 return errors.Annotate(err).Reason("failed to create zlib Reader ").Err() | |
| 41 } | |
| 42 defer zr.Close() | |
| 43 | |
| 44 if err := json.NewDecoder(zr).Decode(v); err != nil { | |
| 45 return errors.Annotate(err).Reason("failed to JSON-decode").Err( ) | |
| 46 } | |
| 47 return nil | |
| 48 } | |
| 49 | |
| 50 // HashParams is a convenience method for hashing a series of strings into a | |
| 51 // unique hash key for that series. | |
|
iannucci
2017/01/07 20:53:16
I would document that the output has a fixed size
dnj
2017/01/10 03:29:17
Done.
| |
| 52 func HashParams(params ...string) []byte { | |
| 53 size := 0 | |
| 54 for i := range params { | |
| 55 size += len(params[i]) + 1 | |
| 56 } | |
| 57 size += binary.MaxVarintLen64 + 1 | |
| 58 | |
| 59 var buf bytes.Buffer | |
| 60 buf.Grow(size) | |
| 61 | |
| 62 // Prefix the hash input with the number of elements. This will prevent some | |
| 63 // inputs that include "0x00" from from stomping over other inputs. | |
| 64 sizeBuf := [binary.MaxVarintLen64]byte{} | |
| 65 _, _ = buf.Write(sizeBuf[:binary.PutUvarint(sizeBuf[:], uint64(len(param s)))]) | |
| 66 buf.WriteByte(0x00) | |
| 67 | |
| 68 // Write each string to the Buffer. | |
| 69 for i, s := range params { | |
| 70 // First, scan through the string to see if it has any "0x00". I t probably | |
| 71 // won't! But if it does, it could create a conflict. If we obse rve this, | |
| 72 // we'll escape "0x00". | |
| 73 if idx := strings.Index(s, "\x00"); idx >= 0 { | |
| 74 _, _ = buf.Write(bytes.Replace([]byte(params[i]), []byte {0x00}, []byte{0x00, 0x00}, -1)) | |
| 75 } else { | |
| 76 _, _ = buf.WriteString(s) | |
| 77 } | |
| 78 buf.WriteByte(0x00) | |
| 79 } | |
| 80 | |
| 81 hash := sha256.Sum256(buf.Bytes()) | |
| 82 return hash[:] | |
| 83 } | |
| OLD | NEW |