List of usage examples for javax.crypto Mac init
public final void init(Key key) throws InvalidKeyException
From source file:jp.primecloud.auto.api.ApiFilter.java
/** * * HMAC-SHA256???// ww w . j a va 2 s .c om * * @param plainText ? * @param keyText * @return * @throws InvalidKeyException * @throws NoSuchAlgorithmException * @throws UnsupportedEncodingException */ private static String encodeSHA256(String plainText, String keyText) throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException { SecretKey secretKey = new SecretKeySpec(keyText.getBytes("UTF-8"), "HmacSHA256"); Mac mac = Mac.getInstance("HmacSHA256"); mac.init(secretKey); byte[] plainBytes = plainText.getBytes("UTF-8"); byte[] encodedBytes = mac.doFinal(plainBytes); byte[] hexBytes = new Hex().encode(encodedBytes); return new String(hexBytes, "UTF-8"); }
From source file:org.wso2.carbon.appfactory.s4.integration.utils.CloudUtils.java
/** * Computes RFC 2104-compliant HMAC signature. * * @param data//w w w .java2 s . c om * The data to be signed. * * @param key * The signing key. * @return * The Base64-encoded RFC 2104-compliant HMAC signature. * @throws AppFactoryException * @throws java.security.SignatureException * when signature generation fails */ public static String calculateRFC2104HMAC(String stringToSign, String secrectAccessKey) throws AppFactoryException { // get an hmac_sha1 key from the raw key bytes SecretKeySpec signingKey = new SecretKeySpec(secrectAccessKey.getBytes(), HMAC_SHA1_ALGORITHM); // get an hmac_sha1 Mac instance and initialize with the signing key Mac mac = null; try { mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); mac.init(signingKey); } catch (NoSuchAlgorithmException e) { String msg = "Error occured while computing RFC 2104-compliant HMAC signature"; log.error(msg, e); throw new AppFactoryException(msg, e); } catch (InvalidKeyException e) { String msg = "Error occured while computing RFC 2104-compliant HMAC signature"; log.error(msg, e); throw new AppFactoryException(msg, e); } // compute the hmac on input data bytes byte[] rawHmac = mac.doFinal(stringToSign.getBytes()); // base64-encode the hmac return new String(Base64.encodeBase64(rawHmac)); }
From source file:cn.crawin.msg.gateway.http.SignUtil.java
/** * ??/*from w w w .j av a 2 s. c o m*/ * * @param secret APP * @param method HttpMethod * @param path * @param headers * @param querys * @param bodys * @param signHeaderPrefixList ???Header? * @return ??? */ public static String sign(String secret, String method, String path, Map<String, String> headers, Map<String, String> querys, Map<String, String> bodys, List<String> signHeaderPrefixList) { try { Mac hmacSha256 = Mac.getInstance(Constants.HMAC_SHA256); byte[] keyBytes = secret.getBytes(Constants.ENCODING); hmacSha256.init(new SecretKeySpec(keyBytes, 0, keyBytes.length, Constants.HMAC_SHA256)); return new String(Base64.encodeBase64( hmacSha256.doFinal(buildStringToSign(method, path, headers, querys, bodys, signHeaderPrefixList) .getBytes(Constants.ENCODING))), Constants.ENCODING); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.example.aliyundemo.ocr.util.SignUtil.java
/** * ??//from ww w .j a va 2 s. c o m * * @param method HttpMethod * @param url Path+Query * @param headers Http * @param formParamMap POST?? * @param secret APP * @param signHeaderPrefixList ???Header? * @return ??? */ public static String sign(String method, String url, Map<String, String> headers, Map formParamMap, String secret, List<String> signHeaderPrefixList) { try { Mac hmacSha256 = Mac.getInstance(Constants.HMAC_SHA256); byte[] keyBytes = secret.getBytes(Constants.ENCODING); hmacSha256.init(new SecretKeySpec(keyBytes, 0, keyBytes.length, Constants.HMAC_SHA256)); return new String(Base64.encodeBase64( hmacSha256.doFinal(buildStringToSign(headers, url, formParamMap, method, signHeaderPrefixList) .getBytes(Constants.ENCODING))), Constants.ENCODING); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:air.com.snagfilms.utils.Utils.java
public static String getfilmriseParameters() { StringBuilder strBlr = null;/*from w w w . java 2s . c o m*/ try { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); long longs = System.currentTimeMillis(); String timestamp = dateFormat.format(longs); ; String stringToSign = timestamp; // Query uses this string // Compute the signature and base64 encode it. String algorithm = "HmacSHA1"; SecretKeySpec key = new SecretKeySpec(SECRET_KEY.getBytes(), algorithm); Mac mac = Mac.getInstance(algorithm); mac.init(key); String signature = new String(Base64.encodeBase64(mac.doFinal(stringToSign.getBytes()))); System.out.println(signature);// required for Query signature = URLEncoder.encode(signature, "UTF-8"); strBlr = new StringBuilder(); strBlr.append(Constants.ACCESS_KEY); strBlr.append("="); strBlr.append(ACCESS_KEY); strBlr.append("&"); strBlr.append(Constants.TIME_STAMP); strBlr.append("="); strBlr.append(timestamp); strBlr.append("&"); strBlr.append(Constants.SIGNATURE); strBlr.append("="); strBlr.append(signature); strBlr.append("&"); strBlr.append(Constants.SITE); strBlr.append("="); strBlr.append(Constants.filmrise); strBlr.append("&"); strBlr.append(Constants.DEVICE); strBlr.append("="); strBlr.append("android"); return strBlr.toString(); } catch (Exception e) { } return null; }
From source file:Main.java
/** * Compute the HMAC with SHA-256 of data, as defined in * http://tools.ietf.org/html/rfc2104#section-2 . * @param key The key byte array.//from ww w. jav a 2s . c o m * @param data The input byte buffer. This does not change the position. * @return The HMAC result. */ public static byte[] computeHmacWithSha256(byte[] key, ByteBuffer data) { final String algorithm = "HmacSHA256"; Mac mac; try { mac = Mac.getInstance(algorithm); } catch (NoSuchAlgorithmException ex) { // Don't expect this to happen. throw new Error("computeHmac: " + algorithm + " is not supported: " + ex.getMessage()); } try { mac.init(new SecretKeySpec(key, algorithm)); } catch (InvalidKeyException ex) { // Don't expect this to happen. throw new Error("computeHmac: Can't init " + algorithm + " with key: " + ex.getMessage()); } int savePosition = data.position(); mac.update(data); data.position(savePosition); return mac.doFinal(); }
From source file:com.ljt.openapi.demo.util.SignUtil.java
/** * ??//from w ww . jav a2 s.c o m * @param secret APP * @param method HttpMethod * @param path * @param headers * @param querys * @param bodys * @param signHeaderPrefixList ???Header? * @return ??? */ public static String sign(String secret, String method, String path, Map<String, String> headers, Map<String, String> querys, Map<String, String> bodys, List<String> signHeaderPrefixList) { try { Mac hmacSha256 = Mac.getInstance(Constants.HMAC_SHA256); byte[] keyBytes = secret.getBytes(Constants.ENCODING); hmacSha256.init(new SecretKeySpec(keyBytes, 0, keyBytes.length, Constants.HMAC_SHA256)); String sign = new String(Base64.encodeBase64( hmacSha256.doFinal(buildStringToSign(method, path, headers, querys, bodys, signHeaderPrefixList) .getBytes(Constants.ENCODING))), Constants.ENCODING); logger.info("sign:" + sign); return sign; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:flpitu88.web.backend.psicoweb.config.Jwt.java
/** * Private method to generate a signature from a key * * @param input Data to sign/* ww w.ja v a2 s.com*/ * @param key Key used for the signature * @param method Algorithm * * @return Signature * * @throws NoSuchAlgorithmException * @throws UnsupportedEncodingException * @throws InvalidKeyException */ private static byte[] sign(String input, String key, String method) throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException { Mac hmac = Mac.getInstance(method); SecretKey secretKey = new SecretKeySpec(key.getBytes(), method); hmac.init(secretKey); return hmac.doFinal(input.getBytes()); }
From source file:com.cloud.utils.SwiftUtil.java
static String calculateRFC2104HMAC(String data, String key) throws SignatureException, NoSuchAlgorithmException, InvalidKeyException { SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM); Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); mac.init(signingKey); return toHexString(mac.doFinal(data.getBytes())); }
From source file:org.jahia.security.TOTP.java
/** * This method uses the JCE to provide the crypto algorithm. HMAC computes a * Hashed Message Authentication Code with the crypto hash algorithm as a * parameter./*from w w w .j ava 2s . c om*/ * * @param crypto * : the crypto algorithm (HmacSHA1, HmacSHA256, HmacSHA512) * @param keyBytes * : the bytes to use for the HMAC key * @param text * : the message or text to be authenticated */ private static byte[] hmac_sha(String crypto, byte[] keyBytes, byte[] text) { try { Mac hmac; hmac = Mac.getInstance(crypto); SecretKeySpec macKey = new SecretKeySpec(keyBytes, "RAW"); hmac.init(macKey); return hmac.doFinal(text); } catch (GeneralSecurityException gse) { throw new UndeclaredThrowableException(gse); } }