OLD | NEW |
1 // Copyright 2011 The Go Authors. All rights reserved. | 1 // Copyright 2011 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // Package bcrypt implements Provos and Mazières's bcrypt adaptive hashing | 5 // Package bcrypt implements Provos and Mazières's bcrypt adaptive hashing |
6 // algorithm. See http://www.usenix.org/event/usenix99/provos/provos.pdf | 6 // algorithm. See http://www.usenix.org/event/usenix99/provos/provos.pdf |
7 package bcrypt | 7 package bcrypt |
8 | 8 |
9 // The code is a port of Provos and Mazières's C implementation.· | 9 // The code is a port of Provos and Mazières's C implementation.· |
10 import ( | 10 import ( |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 type hashed struct { | 76 type hashed struct { |
77 hash []byte | 77 hash []byte |
78 salt []byte | 78 salt []byte |
79 cost uint32 // allowed range is MinCost to MaxCost | 79 cost uint32 // allowed range is MinCost to MaxCost |
80 major byte | 80 major byte |
81 minor byte | 81 minor byte |
82 } | 82 } |
83 | 83 |
84 // GenerateFromPassword returns the bcrypt hash of the password at the given | 84 // GenerateFromPassword returns the bcrypt hash of the password at the given |
85 // cost. If the cost given is less than MinCost, the cost will be set to | 85 // cost. If the cost given is less than MinCost, the cost will be set to |
86 // MinCost, instead. Use CompareHashAndPassword, as defined in this package, | 86 // DefaultCost, instead. Use CompareHashAndPassword, as defined in this package, |
87 // to compare the returned hashed password with its cleartext version. | 87 // to compare the returned hashed password with its cleartext version. |
88 func GenerateFromPassword(password []byte, cost int) ([]byte, error) { | 88 func GenerateFromPassword(password []byte, cost int) ([]byte, error) { |
89 p, err := newFromPassword(password, cost) | 89 p, err := newFromPassword(password, cost) |
90 if err != nil { | 90 if err != nil { |
91 return nil, err | 91 return nil, err |
92 } | 92 } |
93 return p.Hash(), nil | 93 return p.Hash(), nil |
94 } | 94 } |
95 | 95 |
96 // CompareHashAndPassword compares a bcrypt hashed password with its possible | 96 // CompareHashAndPassword compares a bcrypt hashed password with its possible |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 func (p *hashed) String() string { | 273 func (p *hashed) String() string { |
274 return fmt.Sprintf("&{hash: %#v, salt: %#v, cost: %d, major: %c, minor:
%c}", string(p.hash), p.salt, p.cost, p.major, p.minor) | 274 return fmt.Sprintf("&{hash: %#v, salt: %#v, cost: %d, major: %c, minor:
%c}", string(p.hash), p.salt, p.cost, p.major, p.minor) |
275 } | 275 } |
276 | 276 |
277 func checkCost(cost int) error { | 277 func checkCost(cost int) error { |
278 if cost < MinCost || cost > MaxCost { | 278 if cost < MinCost || cost > MaxCost { |
279 return InvalidCostError(cost) | 279 return InvalidCostError(cost) |
280 } | 280 } |
281 return nil | 281 return nil |
282 } | 282 } |
OLD | NEW |