Java Code Examples for org.apache.commons.codec.digest.DigestUtils#sha256()

The following examples show how to use org.apache.commons.codec.digest.DigestUtils#sha256() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HwYunMsgSender.java    From WePush with MIT License 6 votes vote down vote up
/**
 * 构造X-WSSE参数值
 *
 * @param appKey
 * @param appSecret
 * @return
 */
static String buildWsseHeader(String appKey, String appSecret) {
    if (null == appKey || null == appSecret || appKey.isEmpty() || appSecret.isEmpty()) {
        System.out.println("buildWsseHeader(): appKey or appSecret is null.");
        return null;
    }
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    String time = sdf.format(new Date()); //Created
    String nonce = UUID.randomUUID().toString().replace("-", ""); //Nonce

    byte[] passwordDigest = DigestUtils.sha256(nonce + time + appSecret);
    String hexDigest = Hex.encodeHexString(passwordDigest);

    //如果JDK版本是1.8,请加载原生Base64类,并使用如下代码
    String passwordDigestBase64Str = Base64.getEncoder().encodeToString(hexDigest.getBytes()); //PasswordDigest
    //如果JDK版本低于1.8,请加载三方库提供Base64类,并使用如下代码
    //String passwordDigestBase64Str = Base64.encodeBase64String(hexDigest.getBytes(Charset.forName("utf-8"))); //PasswordDigest
    //若passwordDigestBase64Str中包含换行符,请执行如下代码进行修正
    //passwordDigestBase64Str = passwordDigestBase64Str.replaceAll("[\\s*\t\n\r]", "");

    return String.format(WSSE_HEADER_FORMAT, appKey, passwordDigestBase64Str, nonce, time);
}
 
Example 2
Source File: TezClientUtils.java    From tez with Apache License 2.0 5 votes vote down vote up
public static byte[] getLocalSha(Path path, Configuration conf) throws IOException {
  InputStream is = null;
  try {
    is = FileSystem.getLocal(conf).open(path);
    return DigestUtils.sha256(is);
  } finally {
    if (is != null) {
      is.close();
    }
  }
}
 
Example 3
Source File: Transaction.java    From blockchain-java with Apache License 2.0 5 votes vote down vote up
/**
 * 计算交易信息的Hash值
 *
 * @return
 */
public byte[] hash() {
    // 使用序列化的方式对Transaction对象进行深度复制
    byte[] serializeBytes = SerializeUtils.serialize(this);
    Transaction copyTx = (Transaction) SerializeUtils.deserialize(serializeBytes);
    copyTx.setTxId(new byte[]{});
    return DigestUtils.sha256(SerializeUtils.serialize(copyTx));
}
 
Example 4
Source File: RelocalizationUtils.java    From incubator-tez with Apache License 2.0 5 votes vote down vote up
public static byte[] getResourceSha(URI uri, Configuration conf) throws IOException {
  InputStream is = null;
  try {
    is = FileSystem.get(uri, conf).open(new Path(uri));
    return DigestUtils.sha256(is);
  } finally {
    if (is != null) {
      is.close();
    }
  }
}
 
Example 5
Source File: RelocalizationUtils.java    From incubator-tez with Apache License 2.0 5 votes vote down vote up
public static byte[] getLocalSha(Path path, Configuration conf) throws IOException {
  InputStream is = null;
  try {
    is = FileSystem.getLocal(conf).open(path);
    return DigestUtils.sha256(is);
  } finally {
    if (is != null) {
      is.close();
    }
  }
}
 
Example 6
Source File: SQLUtils.java    From cs-actions with Apache License 2.0 5 votes vote down vote up
@NotNull
public static String computeSessionId(@NotNull final String aString) {
    final byte[] byteData = DigestUtils.sha256(aString.getBytes());
    final StringBuilder sb = new StringBuilder("SQLQuery:");

    for (final byte aByteData : byteData) {
        final String hex = Integer.toHexString(0xFF & aByteData);
        if (hex.length() == 1) {
            sb.append('0');
        }
        sb.append(hex);
    }
    return sb.toString();
}
 
Example 7
Source File: KiteConnect.java    From javakiteconnect with MIT License 5 votes vote down vote up
/** Hex encodes sha256 output for android support.
 * @return Hex encoded String.
 * @param str is the String that has to be encrypted.
 * */
public String sha256Hex(String str) {
    byte[] a = DigestUtils.sha256(str);
    StringBuilder sb = new StringBuilder(a.length * 2);
    for(byte b: a)
        sb.append(String.format("%02x", b));
    return sb.toString();
}
 
Example 8
Source File: TezClientUtils.java    From tez with Apache License 2.0 5 votes vote down vote up
public static byte[] getResourceSha(URI uri, Configuration conf) throws IOException {
  InputStream is = null;
  try {
    is = FileSystem.get(uri, conf).open(new Path(uri));
    return DigestUtils.sha256(is);
  } finally {
    if (is != null) {
      is.close();
    }
  }
}
 
Example 9
Source File: Block.java    From jblockchain with Apache License 2.0 5 votes vote down vote up
/**
 * Calculates the hash using relevant fields of this type
 * @return SHA256-hash as raw bytes
 */
public byte[] calculateHash() {
    byte[] hashableData = ArrayUtils.addAll(previousBlockHash, merkleRoot);
    hashableData = ArrayUtils.addAll(hashableData, Longs.toByteArray(tries));
    hashableData = ArrayUtils.addAll(hashableData, Longs.toByteArray(timestamp));
    return DigestUtils.sha256(hashableData);
}
 
Example 10
Source File: Transaction.java    From jblockchain with Apache License 2.0 5 votes vote down vote up
/**
 * Calculates the hash using relevant fields of this type
 * @return SHA256-hash as raw bytes
 */
public byte[] calculateHash() {
    byte[] hashableData = ArrayUtils.addAll(text.getBytes(), senderHash);
    hashableData = ArrayUtils.addAll(hashableData, signature);
    hashableData = ArrayUtils.addAll(hashableData, Longs.toByteArray(timestamp));
    return DigestUtils.sha256(hashableData);
}
 
Example 11
Source File: BtcAddressUtils.java    From blockchain-java with Apache License 2.0 5 votes vote down vote up
/**
 * 计算公钥的 RIPEMD160 Hash值
 *
 * @param pubKey 公钥
 * @return ipeMD160Hash(sha256 ( pubkey))
 */
public static byte[] ripeMD160Hash(byte[] pubKey) {
    //1. 先对公钥做 sha256 处理
    byte[] shaHashedKey = DigestUtils.sha256(pubKey);
    RIPEMD160Digest ripemd160 = new RIPEMD160Digest();
    ripemd160.update(shaHashedKey, 0, shaHashedKey.length);
    byte[] output = new byte[ripemd160.getDigestSize()];
    ripemd160.doFinal(output, 0);
    return output;
}
 
Example 12
Source File: Address.java    From jblockchain with Apache License 2.0 4 votes vote down vote up
/**
 * Calculates the hash using relevant fields of this type
 * @return SHA256-hash as raw bytes
 */
private byte[] calculateHash() {
    byte[] hashableData = ArrayUtils.addAll(name.getBytes(), publicKey);
    return DigestUtils.sha256(hashableData);
}
 
Example 13
Source File: CodecUtils.java    From juice with Apache License 2.0 4 votes vote down vote up
public static byte[] sha256(final InputStream in) throws IOException {
    return DigestUtils.sha256(in);
}
 
Example 14
Source File: CodecUtils.java    From juice with Apache License 2.0 4 votes vote down vote up
public static byte[] sha256(final byte[] data) {
    return DigestUtils.sha256(data);
}
 
Example 15
Source File: Main.java    From softwarecave with GNU General Public License v3.0 4 votes vote down vote up
private static void calculateSha256CommonsIO(Path path) throws IOException {
    try (InputStream is = Files.newInputStream(path)) {
        byte[] hashValue = DigestUtils.sha256(is);
        System.out.printf("SHA256(%s) = %s%n", path, bytesToHex(hashValue));
    }
}
 
Example 16
Source File: TokenBindingID.java    From oxAuth with MIT License 4 votes vote down vote up
public byte[] sha256() {
    return DigestUtils.sha256(raw);
}
 
Example 17
Source File: CodeVerifier.java    From oxAuth with MIT License 4 votes vote down vote up
public static String s256(String codeVerifier) {
    byte[] sha256 = DigestUtils.sha256(codeVerifier);
    return base64UrlEncode(sha256);
}
 
Example 18
Source File: CodecUtils.java    From juice with Apache License 2.0 4 votes vote down vote up
/** SHA-256 **/
public static byte[] sha256(final String data) {
    return DigestUtils.sha256(data);
}
 
Example 19
Source File: BtcAddressUtils.java    From blockchain-java with Apache License 2.0 2 votes vote down vote up
/**
 * 双重Hash
 *
 * @param data
 * @return
 */
public static byte[] doubleHash(byte[] data) {
    return DigestUtils.sha256(DigestUtils.sha256(data));
}
 
Example 20
Source File: MerkleTree.java    From blockchain-java with Apache License 2.0 2 votes vote down vote up
/**
 * 计算内部节点Hash
 *
 * @param leftChildHash
 * @param rightChildHash
 * @return
 */
private byte[] internalHash(byte[] leftChildHash, byte[] rightChildHash) {
    byte[] mergedBytes = ByteUtils.merge(leftChildHash, rightChildHash);
    return DigestUtils.sha256(mergedBytes);
}