Add authorization

New PasswordManager methods for use by SAM
This commit is contained in:
zzz
2015-06-26 20:24:15 +00:00
parent 876729c24e
commit 33672e6a86
4 changed files with 48 additions and 7 deletions

View File

@ -99,6 +99,18 @@ public class PasswordManager {
String shash = _context.getProperty(pfx + PROP_SHASH);
if (shash == null)
return false;
return checkHash(shash, pw);
}
/**
* Check pw against b64 salt+hash, as generated by createHash()
*
* @param shash b64 string
* @param pw plain text non-null, already trimmed
* @return if pw verified
* @since 0.9.22
*/
public boolean checkHash(String shash, String pw) {
byte[] shashBytes = Base64.decode(shash);
if (shashBytes == null || shashBytes.length != SHASH_LENGTH)
return false;
@ -110,6 +122,23 @@ public class PasswordManager {
return DataHelper.eq(hash, pwHash);
}
/**
* Create a salt+hash, to be saved and verified later by verifyHash().
*
* @param pw plain text non-null, already trimmed
* @return salted+hash b64 string
* @since 0.9.22
*/
public String createHash(String pw) {
byte[] salt = new byte[SALT_LENGTH];
_context.random().nextBytes(salt);
byte[] pwHash = _context.keyGenerator().generateSessionKey(salt, DataHelper.getUTF8(pw)).getData();
byte[] shashBytes = new byte[SHASH_LENGTH];
System.arraycopy(salt, 0, shashBytes, 0, SALT_LENGTH);
System.arraycopy(pwHash, 0, shashBytes, SALT_LENGTH, SessionKey.KEYSIZE_BYTES);
return Base64.encode(shashBytes);
}
/**
* Either plain or b64
*