compareHashAndPassword is an optimized wrapper around bcrypt.CompareHashAndPassword that caches successful results in memory to avoid the _very_ high overhead of calling bcrypt.
(cache Cache, hash []byte, password []byte)
| 40 | // compareHashAndPassword is an optimized wrapper around bcrypt.CompareHashAndPassword that |
| 41 | // caches successful results in memory to avoid the _very_ high overhead of calling bcrypt. |
| 42 | func compareHashAndPassword(cache Cache, hash []byte, password []byte) bool { |
| 43 | // Actually we cache the SHA1 digest of the password to avoid keeping passwords in RAM. |
| 44 | key := authKey(hash, password) |
| 45 | if cache.Contains(key) { |
| 46 | return true |
| 47 | } |
| 48 | // Cache missed; now we make the very slow (~100ms) bcrypt call: |
| 49 | if err := bcrypt.CompareHashAndPassword(hash, password); err != nil { |
| 50 | // Note: It's important to only cache successful matches, not failures. |
| 51 | // Failure is supposed to be slow, to make online attacks impractical. |
| 52 | return false |
| 53 | } |
| 54 | cache.Put(key) |
| 55 | return true |
| 56 | } |
| 57 | |
| 58 | // SetBcryptCost will set the bcrypt cost for Sync Gateway to use |
| 59 | // Values of zero or less will use bcryptDefaultCost instead |