List of usage examples for javax.crypto Mac getInstance
public static final Mac getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:br.com.ingenieux.jenkins.plugins.codecommit.RequestSignerBase.java
protected byte[] hash(byte[] kSecret, String obj) { try {/* www .j a va 2s.c o m*/ SecretKeySpec keySpec = new SecretKeySpec(kSecret, "HmacSHA256"); Mac mac = Mac.getInstance("HmacSHA256"); mac.init(keySpec); return mac.doFinal(obj.getBytes(DEFAULT_CHARSET)); } catch (Exception exc) { throw new RuntimeException(exc); } }
From source file:com.cilogi.aws.cart.SignRequests.java
SignRequests(@NonNull EndPoint endpoint, @NonNull String awsAccessKeyId, @NonNull String awsSecretKey) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException { this.endpoint = endpoint; this.awsAccessKeyId = awsAccessKeyId; byte[] secretKeyBytes = awsSecretKey.getBytes(UTF8); SecretKeySpec secretKeySpec = new SecretKeySpec(secretKeyBytes, HMAC_NAME); this.mac = Mac.getInstance(HMAC_NAME); this.mac.init(secretKeySpec); }
From source file:com.torchmind.authenticator.AbstractTokenGenerator.java
/** * Generates a code based on a secret key and challenge. * * @param secretKey a secret key.//from w w w. ja v a2 s . c o m * @param challenge a challenge. * @return a code. */ @NonNull protected String generateCode(@NonNull SecretKey secretKey, @NonNull byte[] challenge) { try { Mac mac = Mac.getInstance("Hmac" + this.algorithm.name()); mac.init(secretKey); byte[] hash = mac.doFinal(challenge); int offset = hash[hash.length - 1] & 0x0F; ByteBuffer buffer = ByteBuffer.allocate(4).put(hash, offset, 4); buffer.flip(); return String.format("%0" + this.digits + "d", (buffer.getInt() & 0x7FFFFFFF) % this.digitModulo); } catch (NoSuchAlgorithmException ex) { throw new UnsupportedOperationException( "The specified algorithm is not supported by this Java VM implementation: " + ex.getMessage(), ex); } catch (InvalidKeyException ex) { throw new IllegalArgumentException("Invalid shared secret: " + ex.getMessage(), ex); } }
From source file:org.brunocvcunha.instagram4j.util.InstagramHashUtil.java
/** * Generate a Hmac SHA-256 hash/* w w w.j a va 2s .c o m*/ * @param key key * @param string value * @return hashed */ public static String generateHash(String key, String string) { SecretKeySpec object = new SecretKeySpec(key.getBytes(), "HmacSHA256"); try { Mac mac = Mac.getInstance("HmacSHA256"); mac.init((Key) object); byte[] byteArray = mac.doFinal(string.getBytes("UTF-8")); return new String(new Hex().encode(byteArray), "ISO-8859-1"); } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:com.swdouglass.joid.Crypto.java
private static byte[] hmacShaX(String keySpec, byte[] key, byte[] text) throws InvalidKeyException, NoSuchAlgorithmException { SecretKey sk = new SecretKeySpec(key, keySpec); Mac m = Mac.getInstance(sk.getAlgorithm()); m.init(sk);//from www . j a v a 2s .c om return m.doFinal(text); }
From source file:jp.co.yahoo.yconnect.core.oidc.JWTVerification.java
/** * HMAC-SHA256????????????// ww w . ja v a 2 s.c om * * @return ????? */ private String generateSignature() { // mac??? Mac sha256_HMAC = null; try { sha256_HMAC = Mac.getInstance("HmacSHA256"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } // secret_key?? SecretKeySpec secret_key = null; try { secret_key = new SecretKeySpec(this.clientSecret.getBytes("UTF-8"), "HmacSHA256"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } // mac?? try { sha256_HMAC.init(secret_key); } catch (InvalidKeyException e) { e.printStackTrace(); } String[] idTokenArray = this.idTokenString.split("\\."); this.dataPart = idTokenArray[0] + "." + idTokenArray[1]; String hash = null; try { hash = Base64.encodeBase64String(sha256_HMAC.doFinal(this.dataPart.getBytes("UTF-8"))); } catch (IllegalStateException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } // URLSafe?Base64?? hash = hash.replace("=", ""); hash = hash.replace("+", "-"); hash = hash.replace("/", "_"); return hash; }
From source file:ch.icclab.cyclops.client.CloudStackAuth.java
/** * Simple SHA1 implementation with Base64 encoding of message * * @param query header to be signed//from w w w .j a v a 2s .c o m * @return signed string * @throws NoSuchAlgorithmException * @throws InvalidKeyException * @throws UnsupportedEncodingException */ private String signRequest(String query) throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException { logger.trace("Signing the CloudStack API query"); Mac sha1_HMAC = Mac.getInstance("HmacSHA1"); SecretKeySpec secret = new SecretKeySpec(apiConnection.getCloudStackSecretKey().getBytes(), "HmacSHA1"); sha1_HMAC.init(secret); // now sign it and return Base64 representation String signature = Base64.encodeBase64String(sha1_HMAC.doFinal(query.getBytes())); return URLEncoder.encode(signature, "UTF-8"); }
From source file:com.tapcentive.minimalist.JWTHelper.java
/*** * Generates a JSON Web Token. This token uses the supplied application key to sign the token * content which should uniquely identify the client's customer (e.g. a loyalty number, email * etc.) and contain any Tapcentive audience IDs assigned to this customer. This example does * not require that either parameter be present. * @param originalBody/*from w w w. j a va 2 s . com*/ * @param audiences * @param customerId * @return */ public String createJWT(JSONObject originalBody, List<String> audiences, String customerId) { try { JSONObject header = new JSONObject(); JSONObject body = new JSONObject(originalBody.toString()); header.put("typ", "JWT"); header.put("alg", "HS256"); body.put("iss", _keyid); if ((audiences != null) && (audiences.size() > 0)) { JSONArray attrArray = new JSONArray(audiences); body.put("audiences", attrArray); } if (customerId != null) { body.put("customer_xid", customerId); } String signedContent = Base64.encodeToString(header.toString().getBytes("UTF-8"), Base64.NO_WRAP) + "." + Base64.encodeToString(body.toString().getBytes("UTF-8"), Base64.NO_WRAP); Mac sha256_HMAC = Mac.getInstance("HmacSHA256"); SecretKeySpec secret_key = new SecretKeySpec(_key.getBytes("UTF-8"), "HmacSHA256"); sha256_HMAC.init(secret_key); String signature = Base64.encodeToString(sha256_HMAC.doFinal(signedContent.getBytes("UTF-8")), Base64.NO_WRAP); return signedContent + "." + signature; } catch (JSONException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } return null; }
From source file:org.apache.hadoop.hive.ql.exec.tez.HivePreWarmProcessor.java
@Override public void run(Map<String, LogicalInput> inputs, Map<String, LogicalOutput> outputs) throws Exception { if (prewarmed) { /* container reuse */ return;/*w w w.j a v a2s . co m*/ } for (LogicalInput input : inputs.values()) { input.start(); } for (LogicalOutput output : outputs.values()) { output.start(); } /* these are things that goes through singleton initialization on most queries */ FileSystem fs = FileSystem.get(conf); Mac mac = Mac.getInstance("HmacSHA1"); ReadaheadPool rpool = ReadaheadPool.getInstance(); ShimLoader.getHadoopShims(); URL hiveurl = new URL("jar:" + DagUtils.getInstance().getExecJarPathLocal() + "!/"); JarURLConnection hiveconn = (JarURLConnection) hiveurl.openConnection(); JarFile hivejar = hiveconn.getJarFile(); try { Enumeration<JarEntry> classes = hivejar.entries(); while (classes.hasMoreElements()) { JarEntry je = classes.nextElement(); if (je.getName().endsWith(".class")) { String klass = je.getName().replace(".class", "").replaceAll("/", "\\."); if (klass.indexOf("ql.exec") != -1 || klass.indexOf("ql.io") != -1) { /* several hive classes depend on the metastore APIs, which is not included * in hive-exec.jar. These are the relatively safe ones - operators & io classes. */ if (klass.indexOf("vector") != -1 || klass.indexOf("Operator") != -1) { JavaUtils.loadClass(klass); } } } } } finally { hivejar.close(); } prewarmed = true; }
From source file:amazon.SignedRequestsHelper.java
/** * You must provide the three values below to initialize the helper. * //w ww .j a va2 s . c o m * @param endpoint Destination for the requests. * @param awsAccessKeyId Your AWS Access Key ID * @param awsSecretKey Your AWS Secret Key */ public static SignedRequestsHelper getInstance(String endpoint, String awsAccessKeyId, String awsSecretKey) throws IllegalArgumentException, UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException { if (null == endpoint || endpoint.length() == 0) { throw new IllegalArgumentException("endpoint is null or empty"); } if (null == awsAccessKeyId || awsAccessKeyId.length() == 0) { throw new IllegalArgumentException("awsAccessKeyId is null or empty"); } if (null == awsSecretKey || awsSecretKey.length() == 0) { throw new IllegalArgumentException("awsSecretKey is null or empty"); } SignedRequestsHelper instance = new SignedRequestsHelper(); instance.endpoint = endpoint.toLowerCase(); instance.awsAccessKeyId = awsAccessKeyId; instance.awsSecretKey = awsSecretKey; byte[] secretyKeyBytes = instance.awsSecretKey.getBytes(UTF8_CHARSET); instance.secretKeySpec = new SecretKeySpec(secretyKeyBytes, HMAC_SHA256_ALGORITHM); instance.mac = Mac.getInstance(HMAC_SHA256_ALGORITHM); instance.mac.init(instance.secretKeySpec); return instance; }