Last active
August 29, 2015 14:00
-
-
Save wang2bo2/7ff0212e2b00ec47b4c3 to your computer and use it in GitHub Desktop.
Revisions
-
wang2bo2 revised this gist
May 3, 2014 . 1 changed file with 6 additions and 12 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -14,20 +14,14 @@ * @author bowang * */ public final class HashUtils { private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1"; private HashUtils() { throw new AssertionError(); } static final String hexString(byte[] raw) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < raw.length; i++) { @@ -37,7 +31,7 @@ static final String hexString(byte[] raw) { return sb.toString(); } public static final String md5(String raw) { try { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(raw.getBytes()); @@ -47,11 +41,11 @@ static final String md5(String raw) { return null; } public static final String sha1(String raw) { return sha1(raw.getBytes()); } public static final String sha1(byte[] bytes) { try { MessageDigest md = MessageDigest.getInstance("SHA1"); md.update(bytes); @@ -61,7 +55,7 @@ static final String sha1(byte[] bytes) { return null; } public static final String sha1Base64(byte[] bytes) { try { MessageDigest md = MessageDigest.getInstance("SHA1"); md.update(bytes); @@ -82,7 +76,7 @@ static final String sha1Base64(byte[] bytes) { * @throws java.security.SignatureException * when signature generation fails */ public static String hmacsha1(String raw, String key) throws SignatureException { try { // get an hmac_sha1 key from the raw key bytes -
wang2bo2 created this gist
May 3, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,106 @@ package com.calciumion.util; import android.util.Base64; import java.security.MessageDigest; import java.security.SignatureException; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; /** * Commonly used hash functions * * @author bowang * */ final class HashUtils { private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1"; private HashUtils() { throw new AssertionError(); } /** * Convert the byte to hex format method 1 * * @param raw * @return */ static final String hexString(byte[] raw) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < raw.length; i++) { sb.append(Integer.toString((raw[i] & 0xff) + 0x100, 16) .substring(1)); } return sb.toString(); } static final String md5(String raw) { try { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(raw.getBytes()); return hexString(md.digest()); } catch (Exception e) { } return null; } static final String sha1(String raw) { return sha1(raw.getBytes()); } static final String sha1(byte[] bytes) { try { MessageDigest md = MessageDigest.getInstance("SHA1"); md.update(bytes); return hexString(md.digest()); } catch (Exception e) { } return null; } static final String sha1Base64(byte[] bytes) { try { MessageDigest md = MessageDigest.getInstance("SHA1"); md.update(bytes); return Base64.encodeToString(md.digest(), Base64.DEFAULT); } catch (Exception e) { } return null; } /** * Computes RFC 2104-compliant HMAC signature. * * @param raw * The data to be signed. * @param key * The signing key. * @return The Base64-encoded RFC 2104-compliant HMAC signature. * @throws java.security.SignatureException * when signature generation fails */ static String hmacsha1(String raw, String key) throws SignatureException { try { // get an hmac_sha1 key from the raw key bytes SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM); // get an hmac_sha1 Mac instance and initialize with the signing key Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); mac.init(signingKey); // compute the hmac on input data bytes byte[] rawHmac = mac.doFinal(raw.getBytes()); return hexString(rawHmac); } catch (Exception e) { throw new SignatureException("Failed to generate HMAC : " + e.getMessage()); } } }