| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package types | 5 package types |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "crypto/rand" | 8 "crypto/rand" |
| 9 » "errors" | 9 » "fmt" |
| 10 ) | 10 ) |
| 11 | 11 |
| 12 const ( | 12 const ( |
| 13 » // StreamSecretLength is the size, in bytes, of the stream secret. | 13 » // PrefixSecretLength is the size, in bytes, of the stream secret. |
| 14 // | 14 // |
| 15 // This value was chosen such that it is: | 15 // This value was chosen such that it is: |
| 16 // - Sufficiently large to avoid collisions. | 16 // - Sufficiently large to avoid collisions. |
| 17 // - Can be expressed as a Base64 string without ugly padding. | 17 // - Can be expressed as a Base64 string without ugly padding. |
| 18 » StreamSecretLength = 36 | 18 » PrefixSecretLength = 36 |
| 19 ) | 19 ) |
| 20 | 20 |
| 21 // StreamSecret is the stream secret value. This is a Base64-encoded secret | 21 // PrefixSecret is the stream secret value. This is a Base64-encoded secret |
| 22 // value. | 22 // value. |
| 23 type StreamSecret []byte | 23 type PrefixSecret []byte |
| 24 | 24 |
| 25 // NewStreamSecret generates a new, default-length secret parameter. | 25 // NewPrefixSecret generates a new, default-length secret parameter. |
| 26 func NewStreamSecret() (StreamSecret, error) { | 26 func NewPrefixSecret() (PrefixSecret, error) { |
| 27 » buf := make([]byte, StreamSecretLength) | 27 » buf := make([]byte, PrefixSecretLength) |
| 28 » count, err := rand.Read(buf) | 28 » if _, err := rand.Read(buf); err != nil { |
| 29 » if err != nil { | |
| 30 return nil, err | 29 return nil, err |
| 31 } | 30 } |
| 32 » if count != len(buf) { | 31 |
| 33 » » return nil, errors.New("streamsecret: Generated secret with inva
lid size") | 32 » value := PrefixSecret(buf) |
| 33 » if err := value.Validate(); err != nil { |
| 34 » » panic(err) |
| 34 } | 35 } |
| 35 » return StreamSecret(buf), nil | 36 » return value, nil |
| 36 } | 37 } |
| 38 |
| 39 // Validate confirms that this prefix secret is conformant. |
| 40 // |
| 41 // Note that this does not scan the byte contents of the secret for any |
| 42 // security-related parameters. |
| 43 func (s PrefixSecret) Validate() error { |
| 44 if len(s) != PrefixSecretLength { |
| 45 return fmt.Errorf("invalid prefix secret length (%d != %d)", len
(s), PrefixSecretLength) |
| 46 } |
| 47 return nil |
| 48 } |
| OLD | NEW |