MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / compareHashAndPassword

Function compareHashAndPassword

auth/password_hash.go:42–56  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.
42func 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

Calls 3

authKeyFunction · 0.85
ContainsMethod · 0.65
PutMethod · 0.65