forked from I2P_Developers/i2p.i2p
Add authorization
New PasswordManager methods for use by SAM
This commit is contained in:
@ -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
|
||||
*
|
||||
|
Reference in New Issue
Block a user