| 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) { |
| 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. |
| 52 // |
| 53 // The output is fixed-size sha256.Size (32) bytes. |
| 54 func HashParams(params ...string) []byte { |
| 55 size := 0 |
| 56 for i := range params { |
| 57 size += len(params[i]) + 1 |
| 58 } |
| 59 size += binary.MaxVarintLen64 + 1 |
| 60 |
| 61 var buf bytes.Buffer |
| 62 buf.Grow(size) |
| 63 |
| 64 // Prefix the hash input with the number of elements. This will prevent
some |
| 65 // inputs that include "0x00" from from stomping over other inputs. |
| 66 sizeBuf := [binary.MaxVarintLen64]byte{} |
| 67 _, _ = buf.Write(sizeBuf[:binary.PutUvarint(sizeBuf[:], uint64(len(param
s)))]) |
| 68 buf.WriteByte(0x00) |
| 69 |
| 70 // Write each string to the Buffer. |
| 71 for i, s := range params { |
| 72 // First, scan through the string to see if it has any "0x00". I
t probably |
| 73 // won't! But if it does, it could create a conflict. If we obse
rve this, |
| 74 // we'll escape "0x00". |
| 75 if idx := strings.Index(s, "\x00"); idx >= 0 { |
| 76 _, _ = buf.Write(bytes.Replace([]byte(params[i]), []byte
{0x00}, []byte{0x00, 0x00}, -1)) |
| 77 } else { |
| 78 _, _ = buf.WriteString(s) |
| 79 } |
| 80 buf.WriteByte(0x00) |
| 81 } |
| 82 |
| 83 hash := sha256.Sum256(buf.Bytes()) |
| 84 return hash[:] |
| 85 } |
| OLD | NEW |