Example usage for org.apache.commons.codec.digest DigestUtils sha256

List of usage examples for org.apache.commons.codec.digest DigestUtils sha256

Introduction

In this page you can find the example usage for org.apache.commons.codec.digest DigestUtils sha256.

Prototype

public static byte[] sha256(String data) 

Source Link

Usage

From source file:nl.arietimmerman.u2f.server.CryptoHelper.java

public static byte[] sha256(byte[] bytes) {
    return DigestUtils.sha256(bytes);
}

From source file:np.com.drose.parkgarau.platform.security.PasswordEncryptor.java

public static String encode(String textToEncode) {
    return Base64.encodeBase64String((byte[]) DigestUtils.sha256((String) textToEncode));
}

From source file:org.apache.ace.authentication.processor.password.PasswordAuthenticationProcessor.java

/**
 * Hashes a given password using the current set hash method.
 * /*from   w  ww .ja  v a  2s . co  m*/
 * @param password the password to hash, should not be <code>null</code>.
 * @return the hashed password, never <code>null</code>.
 */
private Object hashPassword(Object password) {
    if ("none".equalsIgnoreCase(m_passwordHashMethod)) {
        // Very special ROT26 hashing method...
        return password;
    }

    if ("md5".equalsIgnoreCase(m_passwordHashMethod)) {
        if (password instanceof byte[]) {
            return DigestUtils.md5((byte[]) password);
        }
        return DigestUtils.md5((String) password);
    }
    if ("sha1".equalsIgnoreCase(m_passwordHashMethod)) {
        if (password instanceof byte[]) {
            return DigestUtils.sha((byte[]) password);
        }
        return DigestUtils.sha((String) password);
    }
    if ("sha256".equalsIgnoreCase(m_passwordHashMethod)) {
        if (password instanceof byte[]) {
            return DigestUtils.sha256((byte[]) password);
        }
        return DigestUtils.sha256((String) password);
    }
    if ("sha384".equalsIgnoreCase(m_passwordHashMethod)) {
        if (password instanceof byte[]) {
            return DigestUtils.sha384((byte[]) password);
        }
        return DigestUtils.sha384((String) password);
    }
    if ("sha512".equalsIgnoreCase(m_passwordHashMethod)) {
        if (password instanceof byte[]) {
            return DigestUtils.sha512((byte[]) password);
        }
        return DigestUtils.sha512((String) password);
    }
    return password;
}

From source file:org.apache.nifi.registry.web.api.UnsecuredNiFiRegistryClientIT.java

private String calculateSha256Hex(final String narFile) throws IOException {
    try (final InputStream bundleInputStream = new FileInputStream(narFile)) {
        return Hex.toHexString(DigestUtils.sha256(bundleInputStream));
    }//  www  . j ava  2  s  . c om
}

From source file:org.apache.tez.client.TezClientUtils.java

public static byte[] getLocalSha(Path path, Configuration conf) throws IOException {
    InputStream is = null;//from   ww  w  .ja  v  a 2 s . co m
    try {
        is = FileSystem.getLocal(conf).open(path);
        return DigestUtils.sha256(is);
    } finally {
        if (is != null) {
            is.close();
        }
    }
}

From source file:org.apache.tez.client.TezClientUtils.java

public static byte[] getResourceSha(URI uri, Configuration conf) throws IOException {
    InputStream is = null;//  w ww  .j a  v  a 2  s  .c  o  m
    try {
        is = FileSystem.get(uri, conf).open(new Path(uri));
        return DigestUtils.sha256(is);
    } finally {
        if (is != null) {
            is.close();
        }
    }
}

From source file:org.callimachusproject.util.PasswordGenerator.java

public PasswordGenerator(byte[] seed) {
    this(byteToLong(DigestUtils.sha256(seed)));
}

From source file:org.gluu.com.ox_push2.u2f.v2.device.U2FKeyImpl.java

@Override
public EnrollmentResponse register(EnrollmentRequest enrollmentRequest) throws U2FException {
    if (BuildConfig.DEBUG)
        Log.d(TAG, ">> register");

    String application = enrollmentRequest.getApplication();
    String challenge = enrollmentRequest.getChallenge();

    if (BuildConfig.DEBUG)
        Log.d(TAG, "-- Inputs --");
    if (BuildConfig.DEBUG)
        Log.d(TAG, "application: " + application);
    if (BuildConfig.DEBUG)
        Log.d(TAG, "challenge: " + challenge);

    byte userPresent = userPresenceVerifier.verifyUserPresence();
    if ((userPresent & AuthenticateRequest.USER_PRESENT_FLAG) == 0) {
        throw new U2FException("Cannot verify user presence");
    }//from  w w  w .  j  a  va 2  s .  c  o  m

    KeyPair keyPair = keyPairGenerator.generateKeyPair();
    byte[] keyHandle = keyPairGenerator.generateKeyHandle();

    TokenEntry tokenEntry = new TokenEntry(keyPairGenerator.keyPairToJson(keyPair),
            enrollmentRequest.getApplication(), enrollmentRequest.getOxPush2Request().getIssuer());
    tokenEntry.setCreatedDate(enrollmentRequest.getOxPush2Request().getCreated());
    tokenEntry.setUserName(enrollmentRequest.getOxPush2Request().getUserName());
    tokenEntry.setAuthenticationMode(enrollmentRequest.getOxPush2Request().getMethod());
    tokenEntry.setKeyHandle(keyHandle);
    String serverName = tokenEntry.getIssuer();
    String prefixKeyHandle = "Key for ";
    String keyHandleTitle = prefixKeyHandle + " " + serverName;
    tokenEntry.setKeyName(keyHandleTitle);
    final boolean oneStep = Utils.isEmpty(enrollmentRequest.getOxPush2Request().getUserName());
    String authenticationType = oneStep ? ONE_STEP : TWO_STEP;
    tokenEntry.setAuthenticationType(authenticationType);
    dataStore.storeTokenEntry(keyHandle, tokenEntry);

    byte[] userPublicKey = keyPairGenerator.encodePublicKey(keyPair.getPublic());

    byte[] applicationSha256 = DigestUtils.sha256(application);
    byte[] challengeSha256 = DigestUtils.sha256(challenge);
    byte[] signedData = rawMessageCodec.encodeRegistrationSignedBytes(applicationSha256, challengeSha256,
            keyHandle, userPublicKey);
    if (BuildConfig.DEBUG)
        Log.d(TAG, "Signing bytes " + Utils.encodeHexString(signedData));

    byte[] signature = keyPairGenerator.sign(signedData, certificatePrivateKey);

    if (BuildConfig.DEBUG)
        Log.d(TAG, "-- Outputs --");
    if (BuildConfig.DEBUG)
        Log.d(TAG, "userPublicKey: " + Utils.encodeHexString(userPublicKey));
    if (BuildConfig.DEBUG)
        Log.d(TAG, "keyHandle: " + Utils.base64UrlEncode(keyHandle));
    if (BuildConfig.DEBUG)
        Log.d(TAG, "vendorCertificate: " + vendorCertificate);
    if (BuildConfig.DEBUG)
        Log.d(TAG, "signature: " + Utils.encodeHexString(signature));

    if (BuildConfig.DEBUG)
        Log.d(TAG, "<< register");

    return new EnrollmentResponse(userPublicKey, keyHandle, vendorCertificate, signature);
}

From source file:org.gluu.com.ox_push2.u2f.v2.device.U2FKeyImpl.java

@Override
public AuthenticateResponse authenticate(AuthenticateRequest authenticateRequest) throws U2FException {
    if (BuildConfig.DEBUG)
        Log.d(TAG, ">> authenticate");

    byte control = authenticateRequest.getControl();
    String application = authenticateRequest.getApplication();
    String challenge = authenticateRequest.getChallenge();
    byte[] keyHandle = authenticateRequest.getKeyHandle();

    if (BuildConfig.DEBUG)
        Log.d(TAG, "-- Inputs --");
    if (BuildConfig.DEBUG)
        Log.d(TAG, "control: " + control);
    if (BuildConfig.DEBUG)
        Log.d(TAG, "application: " + application);
    if (BuildConfig.DEBUG)
        Log.d(TAG, "challenge: " + challenge);
    if (BuildConfig.DEBUG)
        Log.d(TAG, "keyHandle: " + Utils.base64UrlEncode(keyHandle));

    TokenEntry tokenEntry = dataStore.getTokenEntry(keyHandle);

    if (tokenEntry == null) {
        Log.e(TAG, "There is no keyPair for keyHandle: " + Utils.base64UrlEncode(keyHandle));
        return null;
    }//from w  w  w .  j a  v a2s  .c  o  m

    if (!application.equals(tokenEntry.getApplication())) {
        throw new U2FException("KeyHandle " + Utils.base64UrlEncode(keyHandle)
                + " is associated with application: " + tokenEntry.getApplication());
    }

    String keyPairJson = tokenEntry.getKeyPair();
    if (keyPairJson == null) {
        Log.e(TAG, "There is no keyPair for keyHandle: " + Utils.base64UrlEncode(keyHandle));
        return null;
    }

    KeyPair keyPair;
    try {
        keyPair = keyPairGenerator.keyPairFromJson(keyPairJson);
    } catch (U2FException ex) {
        Log.e(TAG, "There is no keyPair for keyHandle: " + Utils.base64UrlEncode(keyHandle));
        return null;
    }

    int counter = dataStore.incrementCounter(keyHandle);
    byte userPresence = userPresenceVerifier.verifyUserPresence();
    byte[] applicationSha256 = DigestUtils.sha256(application);
    byte[] challengeSha256 = DigestUtils.sha256(challenge);
    byte[] signedData = rawMessageCodec.encodeAuthenticateSignedBytes(applicationSha256, userPresence, counter,
            challengeSha256);

    if (BuildConfig.DEBUG)
        Log.d(TAG, "Signing bytes " + Utils.encodeHexString(signedData));

    byte[] signature = keyPairGenerator.sign(signedData, keyPair.getPrivate());

    if (BuildConfig.DEBUG)
        Log.d(TAG, "-- Outputs --");
    if (BuildConfig.DEBUG)
        Log.d(TAG, "userPresence: " + userPresence);
    if (BuildConfig.DEBUG)
        Log.d(TAG, "counter: " + counter);
    if (BuildConfig.DEBUG)
        Log.d(TAG, "signature: " + Utils.encodeHexString(signature));

    if (BuildConfig.DEBUG)
        Log.d(TAG, "<< authenticate");

    return new AuthenticateResponse(userPresence, counter, signature);
}

From source file:org.gluu.oxpush2.u2f.v2.device.U2FKeyImpl.java

@Override
public EnrollmentResponse register(EnrollmentRequest enrollmentRequest) throws U2FException {
    if (BuildConfig.DEBUG)
        Log.d(TAG, ">> register");

    String application = enrollmentRequest.getApplication();
    String challenge = enrollmentRequest.getChallenge();

    if (BuildConfig.DEBUG)
        Log.d(TAG, "-- Inputs --");
    if (BuildConfig.DEBUG)
        Log.d(TAG, "application: " + application);
    if (BuildConfig.DEBUG)
        Log.d(TAG, "challenge: " + challenge);

    byte userPresent = userPresenceVerifier.verifyUserPresence();
    if ((userPresent & AuthenticateRequest.USER_PRESENT_FLAG) == 0) {
        throw new U2FException("Cannot verify user presence");
    }//from  w  w  w.ja  v a 2 s . c o m

    KeyPair keyPair = keyPairGenerator.generateKeyPair();
    byte[] keyHandle = keyPairGenerator.generateKeyHandle();

    TokenEntry tokenEntry = new TokenEntry(keyPairGenerator.keyPairToJson(keyPair),
            enrollmentRequest.getApplication(), enrollmentRequest.getIssuer());
    dataStore.storeTokenEntry(keyHandle, tokenEntry);

    byte[] userPublicKey = keyPairGenerator.encodePublicKey(keyPair.getPublic());

    byte[] applicationSha256 = DigestUtils.sha256(application);
    byte[] challengeSha256 = DigestUtils.sha256(challenge);
    byte[] signedData = rawMessageCodec.encodeRegistrationSignedBytes(applicationSha256, challengeSha256,
            keyHandle, userPublicKey);
    if (BuildConfig.DEBUG)
        Log.d(TAG, "Signing bytes " + Utils.encodeHexString(signedData));

    byte[] signature = keyPairGenerator.sign(signedData, certificatePrivateKey);

    if (BuildConfig.DEBUG)
        Log.d(TAG, "-- Outputs --");
    if (BuildConfig.DEBUG)
        Log.d(TAG, "userPublicKey: " + Utils.encodeHexString(userPublicKey));
    if (BuildConfig.DEBUG)
        Log.d(TAG, "keyHandle: " + Utils.base64UrlEncode(keyHandle));
    if (BuildConfig.DEBUG)
        Log.d(TAG, "vendorCertificate: " + vendorCertificate);
    if (BuildConfig.DEBUG)
        Log.d(TAG, "signature: " + Utils.encodeHexString(signature));

    if (BuildConfig.DEBUG)
        Log.d(TAG, "<< register");

    return new EnrollmentResponse(userPublicKey, keyHandle, vendorCertificate, signature);
}