Example usage for org.apache.shiro.crypto.hash Hash toHex

List of usage examples for org.apache.shiro.crypto.hash Hash toHex

Introduction

In this page you can find the example usage for org.apache.shiro.crypto.hash Hash toHex.

Prototype

String toHex();

Source Link

Document

Returns the <a href="http://en.wikipedia.org/wiki/Hexadecimal">Hex</a>-formatted String representation of the underlying wrapped byte array.

Usage

From source file:base.web.ShiroDbRealm.java

License:Apache License

/**
 * ??spring/shiro.xml?credentialsMatcher <br/>
 * // w ww. ja v a  2s . c  om
 * ?SHA-256Base64?1024<br/>
 * SHA-256Base64??(44?)Hex??64?<br/>
 * ?SHA-384?SHA-512????
 * 
 * @author Junas.Cheung
 */
public String encrytPassword(String password) {
    // ????
    String hashAlgorithmName = ConfigUtil.getValue("password.hash.algorithm.name");
    int iterations = ConfigUtil.getIntValue("password.iterations");
    boolean isStoredHex = ConfigUtil.getBooleanValue("password.is.stored.hex");
    // 
    DefaultHashService hashService = new DefaultHashService();
    // 
    hashService.setHashAlgorithmName(hashAlgorithmName);
    // 
    hashService.setHashIterations(iterations);
    // ?
    ByteSource byteSource = ByteSource.Util.bytes(password);
    Hash hash = hashService.computeHash(new HashRequest.Builder().setSource(byteSource).build());
    // ?Hash
    return isStoredHex ? hash.toHex() : hash.toBase64();
}

From source file:com.zuoxiaolong.niubi.job.persistent.shiro.HashHelper.java

License:Apache License

public static String getHashedPassword(String password, String salt) {
    Hash hash = new SimpleHash(hashAlgorithm, password, salt);
    return hash.toHex();
}

From source file:de.dominikschadow.javasecurity.hash.SHA512.java

License:Apache License

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);/*from  w ww  . j a  v  a2  s .c  om*/

    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());
}

From source file:org.apache.hadoop.gateway.shirorealm.KnoxLdapRealm.java

License:Apache License

@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());
}

From source file:org.apache.hadoop.gateway.shirorealm.KnoxPamRealm.java

License:Apache License

@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    UsernamePasswordToken upToken = (UsernamePasswordToken) token;
    UnixUser user = null;//  w ww.jav  a2 s.c  om
    try {
        user = (new PAM(this.getService())).authenticate(upToken.getUsername(),
                new String(upToken.getPassword()));
    } catch (PAMException e) {
        handleAuthFailure(token, e.getMessage(), e);
    }
    HashRequest.Builder builder = new HashRequest.Builder();
    Hash credentialsHash = hashService
            .computeHash(builder.setSource(token.getCredentials()).setAlgorithmName(HASHING_ALGORITHM).build());
    /* Coverity Scan CID 1361684 */
    if (credentialsHash == null) {
        handleAuthFailure(token, "Failed to compute hash", null);
    }
    return new SimpleAuthenticationInfo(new UnixUserPrincipal(user), credentialsHash.toHex(),
            credentialsHash.getSalt(), getName());
}

From source file:org.lazulite.boot.autoconfigure.osaam.shiro.sys.user.service.PasswordService.java

License:Apache License

public String encryptPassword(String username, String password, String salt) {
    Hash hash = new SimpleHash("MD5", password, ByteSource.Util.bytes(username + salt),
            shiroProperties.getHashIterations());
    String encodedPassword = hash.toHex();
    return encodedPassword;
}