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

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

Introduction

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

Prototype

public static String sha256Hex(String data) 

Source Link

Usage

From source file:org.apache.rya.mongodb.dao.SimpleMongoDBStorageStrategy.java

public BasicDBObject serializeInternal(final RyaStatement statement) {
    String context = "";
    if (statement.getContext() != null) {
        context = statement.getContext().getData();
    }//  ww  w  .j  a va 2  s .c  o m
    final String id = statement.getSubject().getData() + " " + statement.getPredicate().getData() + " "
            + statement.getObject().getData() + " " + context;
    byte[] bytes = id.getBytes(StandardCharsets.UTF_8);
    try {
        final MessageDigest digest = MessageDigest.getInstance("SHA-1");
        bytes = digest.digest(bytes);
    } catch (final NoSuchAlgorithmException e) {
        LOG.error("Unable to perform SHA-1 on the ID, defaulting to raw bytes.", e);
    }
    if (statement.getMetadata() == null) {
        statement.setStatementMetadata(StatementMetadata.EMPTY_METADATA);
    }
    final BasicDBObject dvObject = DocumentVisibilityAdapter.toDBObject(statement.getColumnVisibility());
    final BasicDBObject doc = new BasicDBObject(ID, new String(Hex.encodeHex(bytes)))
            .append(SUBJECT, statement.getSubject().getData())
            .append(SUBJECT_HASH, DigestUtils.sha256Hex(statement.getSubject().getData()))
            .append(PREDICATE, statement.getPredicate().getData())
            .append(PREDICATE_HASH, DigestUtils.sha256Hex(statement.getPredicate().getData()))
            .append(OBJECT, statement.getObject().getData())
            .append(OBJECT_HASH, DigestUtils.sha256Hex(statement.getObject().getData()))
            .append(OBJECT_TYPE, statement.getObject().getDataType().toString()).append(CONTEXT, context)
            .append(STATEMENT_METADATA, statement.getMetadata().toString())
            .append(DOCUMENT_VISIBILITY, dvObject.get(DOCUMENT_VISIBILITY))
            .append(TIMESTAMP, statement.getTimestamp());
    return doc;
}

From source file:org.apache.tajo.engine.function.string.Digest.java

String digest(byte[] data, String type) throws NoSuchAlgorithmException {
    if ("SHA1".equalsIgnoreCase(type) == true) {
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        return new String(Hex.encodeHex(md.digest(data)));
    } else if ("SHA256".equalsIgnoreCase(type) == true) {
        return DigestUtils.sha256Hex(data);
    } else if ("SHA384".equalsIgnoreCase(type) == true) {
        return DigestUtils.sha384Hex(data);
    } else if ("SHA512".equalsIgnoreCase(type) == true) {
        return DigestUtils.sha512Hex(data);
    } else if ("MD5".equalsIgnoreCase(type) == true) {
        return DigestUtils.md5Hex(data);
    } else if ("MD2".equalsIgnoreCase(type) == true) {
        MessageDigest md = MessageDigest.getInstance("MD2");
        return new String(Hex.encodeHex(md.digest(data)));
    }/*from w w w  .  j a  v  a2s .  c  o  m*/

    throw new NoSuchAlgorithmException("Not supported DigestType");
}

From source file:org.apache.tika.parser.BouncyCastleDigestingParserTest.java

private void addTruth(Path tmp, String algo, Metadata truth) throws IOException {
    String digest = null;// w w w. j  a va 2s  .  co m
    //for now, rely on CommonsDigest for truth
    try (InputStream is = Files.newInputStream(tmp)) {
        if ("MD2".equals(algo)) {
            digest = DigestUtils.md2Hex(is);
        } else if ("MD5".equals(algo)) {
            digest = DigestUtils.md5Hex(is);
        } else if ("SHA1".equals(algo)) {
            digest = DigestUtils.sha1Hex(is);
        } else if ("SHA256".equals(algo)) {
            digest = DigestUtils.sha256Hex(is);
        } else if ("SHA384".equals(algo)) {
            digest = DigestUtils.sha384Hex(is);
        } else if ("SHA512".equals(algo)) {
            digest = DigestUtils.sha512Hex(is);
        } else {
            throw new IllegalArgumentException("Sorry, not aware of algorithm: " + algo);
        }
    }
    truth.set(P + algo, digest);

}

From source file:org.apache.tika.parser.DigestingParserTest.java

private void addTruth(Path tmp, CommonsDigester.DigestAlgorithm algo, Metadata truth) throws IOException {
    String digest = null;//from w  ww  . j a  va 2  s .  c  om
    try (InputStream is = Files.newInputStream(tmp)) {
        switch (algo) {
        case MD2:
            digest = DigestUtils.md2Hex(is);
            break;
        case MD5:
            digest = DigestUtils.md5Hex(is);
            break;
        case SHA1:
            digest = DigestUtils.sha1Hex(is);
            break;
        case SHA256:
            digest = DigestUtils.sha256Hex(is);
            break;
        case SHA384:
            digest = DigestUtils.sha384Hex(is);
            break;
        case SHA512:
            digest = DigestUtils.sha512Hex(is);
            break;
        default:
            throw new IllegalArgumentException("Sorry, not aware of algorithm: " + algo.toString());
        }
    }
    truth.set(P + algo.name(), digest);

}

From source file:org.apache.tika.parser.utils.CommonsDigester.java

/**
 *
 * @param algorithm algo to use// w ww .ja v a  2s  .  co m
 * @param is input stream to read from
 * @param metadata metadata for reporting the digest
 * @return whether or not this finished the input stream
 * @throws IOException
 */
private boolean digestEach(DigestAlgorithm algorithm, InputStream is, Metadata metadata) throws IOException {
    String digest = null;
    try {
        switch (algorithm) {
        case MD2:
            digest = DigestUtils.md2Hex(is);
            break;
        case MD5:
            digest = DigestUtils.md5Hex(is);
            break;
        case SHA1:
            digest = DigestUtils.sha1Hex(is);
            break;
        case SHA256:
            digest = DigestUtils.sha256Hex(is);
            break;
        case SHA384:
            digest = DigestUtils.sha384Hex(is);
            break;
        case SHA512:
            digest = DigestUtils.sha512Hex(is);
            break;
        default:
            throw new IllegalArgumentException("Sorry, not aware of algorithm: " + algorithm.toString());
        }
    } catch (IOException e) {
        e.printStackTrace();
        //swallow, or should we throw this?
    }
    if (is instanceof SimpleBoundedInputStream) {
        if (((SimpleBoundedInputStream) is).hasHitBound()) {
            return false;
        }
    }
    metadata.set(algorithm.getMetadataKey(), digest);
    return true;
}

From source file:org.apt.demo.util.Tools.java

public static String encodeMD5(String s) {
    String res = "";
    try {//from   w  w w. j a va 2s .c  o m
        DigestUtils util = new DigestUtils();
        res = new String(util.sha256Hex(s.getBytes()));
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return res;
}

From source file:org.boaboa.utils.SecurityUtils.java

public static String sha256(String texto) {
    String resultado = "";
    try {/*from   w  w w.j  a  va 2 s .co m*/
        if (!StringUtils.isEmpty(texto)) {
            resultado = DigestUtils.sha256Hex(texto);
        }
    } catch (Exception e) {
        logger.error(e.toString());
    }
    return resultado;
}

From source file:org.candlepin.common.config.EncryptedConfiguration.java

public void toDecrypt(String property) throws ConfigurationException {
    if (passphrase == null) {
        log.debug("Passphrase is null.  Skipping decrypt.");
        return;//  w w  w.ja v  a  2 s . c  om
    }

    if (!containsKey(property)) {
        log.debug("Can't decrypt missing property: {}", property);
        return;
    }

    String toDecrypt = getString(property);
    if (!toDecrypt.startsWith("$1$")) {
        // this is not an encrypted password, just return it
        log.debug("Value for {} is not an encrypted string", property);
        return;
    }

    // remove the magic string
    toDecrypt = toDecrypt.substring(3);

    try {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

        // NOTE: we are creating a 64byte digest here,
        // but only the first 16 bytes are used as the iv
        String ivString = passphrase + passphrase;
        String iv = DigestUtils.sha256Hex(ivString);
        String passphraseDigest = DigestUtils.sha256Hex(passphrase);

        // FIXME: katello-password creates a 64 byte key, but somewhere
        // it gets truncated to 32 bytes, so we have to do that here.
        SecretKeySpec spec = new SecretKeySpec(Arrays.copyOfRange(passphraseDigest.getBytes(), 0, 32), "AES");

        cipher.init(Cipher.DECRYPT_MODE, spec, new IvParameterSpec(iv.getBytes(), 0, 16));

        // NOTE: the encrypted password is stored hex base64
        byte[] b64bytes = Base64.decodeBase64(toDecrypt);
        String decrypted = new String(cipher.doFinal(b64bytes));
        setProperty(property, decrypted);
    } catch (Exception e) {
        log.error("Failure trying to decrypt value of {}", property, e);
        throw new ConfigurationException(e);
    }

}

From source file:org.candlepin.config.EncryptedValueConfigurationParser.java

public String decryptValue(String toDecrypt, String passphrase) {
    log.info("decrypt called");
    if (!toDecrypt.startsWith("$1$")) {
        // this is not an ecnrypted password, just return it
        log.debug("this is not an encrypted string");
        return toDecrypt;
    }/*from   w  ww  . ja  v a  2  s. com*/

    // remove the magic string
    toDecrypt = toDecrypt.substring(3);

    try {
        Cipher cipher;
        cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

        // NOTE: we are creating a 64byte digest here,
        // but only the first 16 bytes are used as the iv
        String ivString = passphrase + passphrase;
        String iv = DigestUtils.sha256Hex(ivString);
        String passphraseDigest = DigestUtils.sha256Hex(passphrase);

        // FIXME: katello-password creates a 64 byte key, but somewhere
        // it gets truncated to 32 bytes, so we have to do that here.
        SecretKeySpec spec = new SecretKeySpec(Arrays.copyOfRange(passphraseDigest.getBytes(), 0, 32), "AES");

        cipher.init(Cipher.DECRYPT_MODE, spec, new IvParameterSpec(iv.getBytes(), 0, 16));

        log.info("gh10");
        // NOTE: the encrypted password is stored hex base64
        byte[] b64bytes = Base64.decodeBase64(toDecrypt);
        String plaintext = new String(cipher.doFinal(b64bytes));

        return plaintext;
    } catch (Exception e) {
        log.info("Failure trying to decrypt" + toDecrypt, e);
        throw new RuntimeException(e);
    }
}

From source file:org.candlepin.policy.js.compliance.hash.Hasher.java

/**
 * Products an SHA256 hash of anything that was put into this hasher.
 *
 * @return an SHA256 hex string//ww  w.  j  a v a 2  s.  c  o m
 */
public String hash() {
    String data = sink.toString();
    log.debug(data);
    return DigestUtils.sha256Hex(data);
}