Example usage for org.apache.shiro.crypto.hash AbstractHash setBytes

List of usage examples for org.apache.shiro.crypto.hash AbstractHash setBytes

Introduction

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

Prototype

public void setBytes(byte[] alreadyHashedBytes) 

Source Link

Document

Sets the raw bytes stored by this hash instance.

Usage

From source file:xyz.ivyxjc.HashedMatcher.java

License:Apache License

/**
 * Returns a {@link Hash Hash} instance representing the already-hashed AuthenticationInfo credentials stored in the system.
 * <p/>//from ww w .  ja v a 2  s  .c  o m
 * This method reconstructs a {@link Hash Hash} instance based on a {@code info.getCredentials} call,
 * but it does <em>not</em> hash that value - it is expected that method call will return an already-hashed value.
 * <p/>
 * This implementation's reconstruction effort functions as follows:
 * <ol>
 * <li>Convert {@code account.getCredentials()} to a byte array via the {@link #toBytes toBytes} method.
 * <li>If {@code account.getCredentials()} was originally a String or char[] before {@code toBytes} was
 * called, check for encoding:
 * <li>If {@link #storedCredentialsHexEncoded storedCredentialsHexEncoded}, Hex decode that byte array, otherwise
 * Base64 decode the byte array</li>
 * <li>Set the byte[] array directly on the {@code Hash} implementation and return it.</li>
 * </ol>
 *
 * @param info the AuthenticationInfo from which to retrieve the credentials which assumed to be in already-hashed form.
 * @return a {@link Hash Hash} instance representing the given AuthenticationInfo's stored credentials.
 */
protected Object getCredentials(AuthenticationInfo info) {
    Object credentials = info.getCredentials();

    byte[] storedBytes = toBytes(credentials);

    if (credentials instanceof String || credentials instanceof char[]) {
        //account.credentials were a char[] or String, so
        //we need to do text decoding first:
        if (isStoredCredentialsHexEncoded()) {
            storedBytes = Hex.decode(storedBytes);
        } else {
            storedBytes = Base64.decode(storedBytes);
        }
    }
    AbstractHash hash = newHashInstance();
    hash.setBytes(storedBytes);
    return hash;
}