本文整理汇总了Java中org.apache.shiro.crypto.hash.Hash类的典型用法代码示例。如果您正苦于以下问题:Java Hash类的具体用法?Java Hash怎么用?Java Hash使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Hash类属于org.apache.shiro.crypto.hash包,在下文中一共展示了Hash类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: calculateHash
import org.apache.shiro.crypto.hash.Hash; //导入依赖的package包/类
private static Hash calculateHash(String password) {
ByteSource privateSalt = ByteSource.Util.bytes(PRIVATE_SALT_BYTES);
DefaultHashService hashService = new DefaultHashService();
hashService.setPrivateSalt(privateSalt);
hashService.setGeneratePublicSalt(true);
hashService.setHashIterations(ITERATIONS);
HashRequest.Builder builder = new HashRequest.Builder();
builder.setSource(ByteSource.Util.bytes(password));
Hash hash = hashService.computeHash(builder.build());
log.info("Hash algorithm {}, iterations {}, public salt {}", hash.getAlgorithmName(), hash.getIterations(), hash.getSalt());
return hash;
}
示例2: format
import org.apache.shiro.crypto.hash.Hash; //导入依赖的package包/类
public String format(Hash hash) {
if (hash == null) {
return null;
}
String algorithmName = hash.getAlgorithmName();
ByteSource salt = hash.getSalt();
int iterations = hash.getIterations();
StringBuilder sb = new StringBuilder(MCF_PREFIX).append(algorithmName).append(TOKEN_DELIMITER).append(iterations).append(TOKEN_DELIMITER);
if (salt != null) {
sb.append(salt.toBase64());
}
sb.append(TOKEN_DELIMITER);
sb.append(hash.toBase64());
return sb.toString();
}
示例3: passwordsMatch
import org.apache.shiro.crypto.hash.Hash; //导入依赖的package包/类
public boolean passwordsMatch(Object plaintext, Hash saved) {
ByteSource plaintextBytes = createByteSource(plaintext);
if (saved == null || saved.isEmpty()) {
return plaintextBytes == null || plaintextBytes.isEmpty();
} else {
if (plaintextBytes == null || plaintextBytes.isEmpty()) {
return false;
}
}
HashRequest request = buildHashRequest(plaintextBytes, saved);
Hash computed = this.hashService.computeHash(request);
return saved.equals(computed);
}
示例4: doCredentialsMatch
import org.apache.shiro.crypto.hash.Hash; //导入依赖的package包/类
public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
PasswordService service = ensurePasswordService();
Object submittedPassword = getSubmittedPassword(token);
Object storedCredentials = getStoredPassword(info);
assertStoredCredentialsType(storedCredentials);
if (storedCredentials instanceof Hash) {
Hash hashedPassword = (Hash)storedCredentials;
HashingPasswordService hashingService = assertHashingPasswordService(service);
return hashingService.passwordsMatch(submittedPassword, hashedPassword);
}
//otherwise they are a String (asserted in the 'assertStoredCredentialsType' method call above):
String formatted = (String)storedCredentials;
return passwordService.passwordsMatch(submittedPassword, formatted);
}
示例5: doGetAuthenticationInfo
import org.apache.shiro.crypto.hash.Hash; //导入依赖的package包/类
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
if (token instanceof UsernamePasswordToken) {
final UsernamePasswordToken upToken = (UsernamePasswordToken)token;
final String username = upToken.getUsername();
final UserStorage storage = UsersPlugin.getInstance().getStorage();
final DB db = storage.getTxMaker().makeTx();
try {
final User user = storage.getUserMap(db).get(username);
if (user == null) {
return null;
}
Hash hash = new Shiro1CryptFormat().parse(user.getHashedPassword());
return new UserInfo(user.getUsername(), hash);
} finally {
db.close();
}
}
return null;
}
示例6: verifyPassword
import org.apache.shiro.crypto.hash.Hash; //导入依赖的package包/类
private static boolean verifyPassword(byte[] originalHash, ByteSource publicSalt, String password) {
ByteSource privateSalt = ByteSource.Util.bytes(PRIVATE_SALT_BYTES);
DefaultHashService hashService = new DefaultHashService();
hashService.setPrivateSalt(privateSalt);
hashService.setHashIterations(ITERATIONS);
HashRequest.Builder builder = new HashRequest.Builder();
builder.setSource(ByteSource.Util.bytes(password));
builder.setSalt(publicSalt);
Hash comparisonHash = hashService.computeHash(builder.build());
log.info("password: {}", password);
log.info("1 hash: {}", Hex.encodeToString(originalHash));
log.info("2 hash: {}", comparisonHash.toHex());
return Arrays.equals(originalHash, comparisonHash.getBytes());
}
示例7: hashPassword
import org.apache.shiro.crypto.hash.Hash; //导入依赖的package包/类
public Hash hashPassword(Object plaintext) {
ByteSource plaintextBytes = createByteSource(plaintext);
if (plaintextBytes == null || plaintextBytes.isEmpty()) {
return null;
}
HashRequest request = createHashRequest(plaintextBytes);
return hashService.computeHash(request);
}
示例8: buildHashRequest
import org.apache.shiro.crypto.hash.Hash; //导入依赖的package包/类
protected HashRequest buildHashRequest(ByteSource plaintext, Hash saved) {
//keep everything from the saved hash except for the source:
return new HashRequest.Builder().setSource(plaintext)
//now use the existing saved data:
.setAlgorithmName(saved.getAlgorithmName())
.setSalt(saved.getSalt())
.setIterations(saved.getIterations())
.build();
}
示例9: assertStoredCredentialsType
import org.apache.shiro.crypto.hash.Hash; //导入依赖的package包/类
private void assertStoredCredentialsType(Object credentials) {
if (credentials instanceof String || credentials instanceof Hash) {
return;
}
String msg = "Stored account credentials are expected to be either a " +
Hash.class.getName() + " instance or a formatted hash String.";
throw new IllegalArgumentException(msg);
}
示例10: testHash
import org.apache.shiro.crypto.hash.Hash; //导入依赖的package包/类
@Test
public void testHash() {
String password = "testpassword";
Hash hash = underTest.hashPassword(password);
assertThat(underTest.passwordsMatch(password, hash), is(true));
}
示例11: computeHash
import org.apache.shiro.crypto.hash.Hash; //导入依赖的package包/类
@Override
public Hash computeHash(HashRequest request) {
SimpleHash result = new SimpleHash(request.getAlgorithmName());
result.setSalt(request.getSalt());
result.setIterations(request.getIterations());
result.setBytes(request.getSource().getBytes());
return result;
}
示例12: createAuthenticationInfo
import org.apache.shiro.crypto.hash.Hash; //导入依赖的package包/类
@Override
protected AuthenticationInfo createAuthenticationInfo(AuthenticationToken token,
Object ldapPrincipal,
Object ldapCredentials, LdapContext ldapContext) throws NamingException {
HashRequest.Builder builder = new HashRequest.Builder();
Hash credentialsHash = hashService
.computeHash(builder.setSource(token.getCredentials())
.setAlgorithmName(HASHING_ALGORITHM).build());
return new SimpleAuthenticationInfo(token.getPrincipal(),
credentialsHash.toHex(), credentialsHash.getSalt(),
getName());
}
示例13: setPassword
import org.apache.shiro.crypto.hash.Hash; //导入依赖的package包/类
public void setPassword(ByteSource password, long minstolive) {
timeout = System.currentTimeMillis() + minstolive * 60 * 1000;
HashRequest request = new HashRequest.Builder()
.setSource(password)
.setSalt( getSalt() )
.build();
Hash hash = realm.getHashService().computeHash(request);
this.password = hash.toHex();
}
示例14: main
import org.apache.shiro.crypto.hash.Hash; //导入依赖的package包/类
public static void main(String[] args) {
String password = "SHA-512 hash sample text";
Hash hash = calculateHash(password);
boolean correct = verifyPassword(hash.getBytes(), hash.getSalt(), password);
log.info("Entered password is correct: {}", correct);
}
示例15: getCredentialsSalt
import org.apache.shiro.crypto.hash.Hash; //导入依赖的package包/类
@Transient
@Override
public ByteSource getCredentialsSalt() {
return Hash.Util.bytes(passwordSalt);
}