Chromium Code Reviews| 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 "errors" |
| 10 "fmt" | |
| 10 ) | 11 ) |
| 11 | 12 |
| 12 const ( | 13 const ( |
| 13 » // StreamSecretLength is the size, in bytes, of the stream secret. | 14 » // PrefixSecretLength is the size, in bytes, of the stream secret. |
| 14 // | 15 // |
| 15 // This value was chosen such that it is: | 16 // This value was chosen such that it is: |
| 16 // - Sufficiently large to avoid collisions. | 17 // - Sufficiently large to avoid collisions. |
| 17 // - Can be expressed as a Base64 string without ugly padding. | 18 // - Can be expressed as a Base64 string without ugly padding. |
| 18 » StreamSecretLength = 36 | 19 » PrefixSecretLength = 36 |
| 19 ) | 20 ) |
| 20 | 21 |
| 21 // StreamSecret is the stream secret value. This is a Base64-encoded secret | 22 // PrefixSecret is the stream secret value. This is a Base64-encoded secret |
| 22 // value. | 23 // value. |
| 23 type StreamSecret []byte | 24 type PrefixSecret []byte |
| 24 | 25 |
| 25 // NewStreamSecret generates a new, default-length secret parameter. | 26 // NewPrefixSecret generates a new, default-length secret parameter. |
| 26 func NewStreamSecret() (StreamSecret, error) { | 27 func NewPrefixSecret() (PrefixSecret, error) { |
| 27 » buf := make([]byte, StreamSecretLength) | 28 » buf := make([]byte, PrefixSecretLength) |
| 28 count, err := rand.Read(buf) | 29 count, err := rand.Read(buf) |
| 29 if err != nil { | 30 if err != nil { |
| 30 return nil, err | 31 return nil, err |
| 31 } | 32 } |
| 32 if count != len(buf) { | 33 if count != len(buf) { |
|
Ryan Tseng
2016/04/28 20:52:04
Or just use validate() now that you have one?
dnj
2016/04/30 02:51:35
Done.
| |
| 33 » » return nil, errors.New("streamsecret: Generated secret with inva lid size") | 34 » » return nil, errors.New("generated secret with invalid size") |
| 34 } | 35 } |
| 35 » return StreamSecret(buf), nil | 36 » return PrefixSecret(buf), 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 |