List of usage examples for javax.crypto Mac init
public final void init(Key key) throws InvalidKeyException
From source file:OAUTHnesia.java
private static String sha1(String s, String keyString) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException { SecretKeySpec key = new SecretKeySpec((keyString).getBytes("UTF-8"), "HmacSHA1"); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(key); byte[] bytes = mac.doFinal(s.getBytes("UTF-8")); return new String(Base64.encode(bytes, Base64.DEFAULT)); }
From source file:Networking.Server.java
public static String calculateHMAC(String data, byte[] key) { Mac mac = null; byte[] res = null; try {/*from w ww. ja v a 2 s.co m*/ SecretKeySpec signingKey = new SecretKeySpec(key, "HmacSHA1"); mac = Mac.getInstance("HmacSHA1"); mac.init(signingKey); res = (mac.doFinal(data.getBytes())); } catch (InvalidKeyException | NoSuchAlgorithmException ex) { Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex); } return toHexString(res); }
From source file:air.com.snagfilms.cast.chromecast.utils.Utils.java
public static String getfilmriseParameters(Context ctx) { StringBuilder strBlr = null;/*from w w w .java 2 s . c om*/ 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:com.github.benyzhous.springboot.web.core.gateway.sign.backend.Sign.java
/** * HTTP??//from www . jav a 2s . com * * @param uri HTTPPATH??Query * @param httpMethod HTTP * @param headers HTTP * @param paramsMap HTTPQuery+Form? * @param inputStreamBytes HTTPBodyPOST/PUT????,????paramsMap * @return ?? * @throws Exception */ public static String serviceSign(String uri, String httpMethod, Map<String, String> headers, Map<String, Object> paramsMap, byte[] inputStreamBytes) throws Exception { Map<String, String> headersToSign = buildHeadersToSign(headers); String bodyMd5 = buildBodyMd5(httpMethod, inputStreamBytes); String resourceToSign = buildResource(uri, paramsMap); String stringToSign = buildStringToSign(headersToSign, resourceToSign, httpMethod, bodyMd5); Mac hmacSha256 = Mac.getInstance(HMAC_SHA256); String secret = signSecretMap.get(headers.get( HTTP_HEADER_TO_LOWER_CASE ? CA_PROXY_SIGN_SECRET_KEY.toLowerCase() : CA_PROXY_SIGN_SECRET_KEY)); byte[] keyBytes = secret.getBytes(ENCODING); hmacSha256.init(new SecretKeySpec(keyBytes, 0, keyBytes.length, HMAC_SHA256)); return new String(Base64.encodeBase64(hmacSha256.doFinal(stringToSign.getBytes(ENCODING))), ENCODING); }
From source file:org.apache.nifi.processors.standard.util.crypto.scrypt.Scrypt.java
/** * Implementation of PBKDF2 (RFC2898)./* w w w. j a v a2 s .co m*/ * * @param alg the HMAC algorithm to use * @param p the password * @param s the salt * @param c the iteration count * @param dkLen the intended length, in octets, of the derived key * @return The derived key */ private static byte[] pbkdf2(String alg, byte[] p, byte[] s, int c, int dkLen) throws GeneralSecurityException { Mac mac = Mac.getInstance(alg); mac.init(new SecretKeySpec(p, alg)); byte[] dk = new byte[dkLen]; pbkdf2(mac, s, c, dk, dkLen); return dk; }
From source file:crow.weibo.util.WeiboUtil.java
/** * Sina tencent HmacSHA1 ???/*from w ww . j av a 2 s .com*/ * * @param base * ??? * @param consumerSecret * ??API Secret * @param accessTokenSecret * ?? Access Secret * @return */ public static String hmacSHA1Signature(String base, String consumerSecret, String accessTokenSecret) { String HMAC_SHA1 = "HmacSHA1"; try { Mac mac = Mac.getInstance(HMAC_SHA1); String oauthSignature = encode(consumerSecret) + "&" + ((accessTokenSecret == null) ? "" : encode(accessTokenSecret)); SecretKeySpec spec = new SecretKeySpec(oauthSignature.getBytes(), HMAC_SHA1); mac.init(spec); byte[] bytes = mac.doFinal(base.getBytes()); return new String(BASE64Encoder.encode(bytes)); } catch (Exception e) { } return null; }
From source file:com.comcast.cmb.common.util.AuthUtil.java
public static String generateSignature(URL url, Map<String, String> parameters, String version, String algorithm, String accessSecret) throws Exception { String data = null;/*from ww w . jav a2 s .c o m*/ if (version.equals("1")) { data = constructV1DataToSign(parameters); } else if (version.equals("2")) { parameters.put("SignatureMethod", algorithm); data = constructV2DataToSign(url, parameters); } else { return null; } Mac mac = Mac.getInstance(algorithm); mac.init(new SecretKeySpec(accessSecret.getBytes("UTF-8"), algorithm)); byte[] bytes = mac.doFinal(data.getBytes("UTF-8")); String signature = new String(Base64.encodeBase64(bytes)); return signature; }
From source file:com.comcast.cmb.common.util.AuthUtil.java
protected static byte[] sign(byte[] data, byte[] key, SigningAlgorithm algorithm) throws AmazonClientException { try {/* w w w . ja v a 2 s . c o m*/ Mac mac = Mac.getInstance(algorithm.toString()); mac.init(new SecretKeySpec(key, algorithm.toString())); return mac.doFinal(data); } catch (Exception e) { throw new AmazonClientException("Unable to calculate a request signature: " + e.getMessage(), e); } }
From source file:com.otaupdater.utils.Utils.java
public static String hmac(String str, String key) { try {/*from ww w.j a va2 s . com*/ Mac mac = Mac.getInstance(Config.HMAC_ALGORITHM); String salt = randomSaltString(mac.getMacLength()); mac.init(new SecretKeySpec(key.getBytes(), mac.getAlgorithm())); return byteArrToStr(mac.doFinal((salt + str + salt).getBytes("UTF-8"))) + salt; } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:be.cytomine.client.HttpClient.java
public static Header[] authorizeFromRETRIEVAL(String action, String urlFullStr, String contentType, String accept, String publicKey, String privateKey, String host) throws IOException { log.debug("authorize: action=" + action + ", url=" + urlFullStr + ", contentType=" + contentType + ",accept=" + accept); String url = urlFullStr.replace(host, ""); log.debug("authorize: url short=" + url); Header[] headers = new Header[3]; headers[0] = new BasicHeader("accept", accept); headers[1] = new BasicHeader("date", getActualDateStr()); String canonicalHeaders = action + "\n\n" + contentType + "\n" + headers[1].getValue() + "\n"; String messageToSign = canonicalHeaders + url; SecretKeySpec privateKeySign = new SecretKeySpec(privateKey.getBytes(), "HmacSHA1"); try {//from w ww .j av a2 s .co m Mac mac = Mac.getInstance("HmacSHA1"); mac.init(privateKeySign); byte[] rawHmac = mac.doFinal(new String(messageToSign.getBytes(), "UTF-8").getBytes()); //byte[] signatureBytes = Base64.encodeToByte(rawHmac,false); byte[] signatureBytes = org.apache.commons.codec.binary.Base64.encodeBase64(rawHmac, false); String authorization = "CYTOMINE " + publicKey + ":" + new String(signatureBytes); headers[2] = new BasicHeader("authorization", authorization); } catch (GeneralSecurityException e) { throw new IOException(e); } return headers; }