public static byte[] createUserPasswordHash(String userName, char[] passwordChars) { // 不能用用户名和密码组成hash,否则重命名用户后将不能通过原来的密码登录 // TODO 如果不用固定的名称是否还有更好办法? userName = Constants.PROJECT_NAME; if (userName.length() == 0 && passwordChars.length == 0) { return new byte[0]; } return SHA256.getKeyPasswordHash(userName, passwordChars); }
/** * Check the password of this user. * * @param userPasswordHash the password data (the user password hash) * @return true if the user password hash is correct */ public boolean validateUserPasswordHash(byte[] userPasswordHash) { if (userPasswordHash.length == 0 && passwordHash.length == 0) { return true; } if (userPasswordHash.length == 0) { userPasswordHash = SHA256.getKeyPasswordHash(getName(), new char[0]); } byte[] hash = SHA256.getHashWithSalt(userPasswordHash, salt); return Utils.compareSecure(hash, passwordHash); }
private void initStore() { Database db = session.getDatabase(); byte[] key = null; if (cipher != null && password != null) { char[] pass = password.optimize(session).getValue(session).getString().toCharArray(); key = SHA256.getKeyPasswordHash("script", pass); } String file = getFileName(); fileStorage = FileStorage.open(db, file, "rw", cipher, key); fileStorage.setCheckedWriting(false); fileStorage.init(); }