제목 그대로 HMAC[Hash-based Message Authentication Code]의 code
public class Signature { private static final Logger logger = LoggerFactory.getLogger(Signature.class); private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1";
public static String doHMAC(String data, String key) { String result = null;
// 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; try { mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); mac.init(signingKey); // compute the hmac on input data bytes byte[] rawHmac = mac.doFinal(data.getBytes());
// base64-encode the hmac result = encodeBase64(rawHmac); } catch (NoSuchAlgorithmException e) { logger.error("NoSuchAlgorithmException ", e); } catch (InvalidKeyException e) { logger.error("InvalidKeyException ",e); }
return result; } /** * @author Juseok * @see Signature.encodeBase64 * @description : * @param rawHmac * @return */
public static String encodeBase64(byte[] rawData) { String result; byte[] resultArray = Base64.encodeBase64(rawData); result = new String(resultArray); return result; } } |