Skip to content

Commit

Permalink
Use constant-time equality for checking passwords
Browse files Browse the repository at this point in the history
  • Loading branch information
adamw committed Dec 16, 2016
1 parent ad3d7d7 commit cd92362
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
Expand Up @@ -82,4 +82,19 @@ object Utils {
sb.toString()
}

// Do not change this unless you understand the security issues behind timing attacks.
// This method intentionally runs in constant time if the two strings have the same length.
// If it didn't, it would be vulnerable to a timing attack.
def constantTimeEquals(a: String, b: String): Boolean = {
if (a.length != b.length) {
false
}
else {
var equal = 0
for (i <- Array.range(0, a.length)) {
equal |= a(i) ^ b(i)
}
equal == 0
}
}
}
Expand Up @@ -37,7 +37,9 @@ object User {
}

def passwordsMatch(plainPassword: String, user: User): Boolean = {
user.password.equals(encryptPassword(plainPassword, user.salt))
Utils.constantTimeEquals(
user.password,
encryptPassword(plainPassword, user.salt))
}
}

Expand Down

0 comments on commit cd92362

Please sign in to comment.