List of usage examples for javax.crypto Mac getInstance
public static final Mac getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:co.edu.uniandes.csw.Arquidalgos.usuario.service._UsuarioService.java
@POST @Path("/crearUsuario") public UsuarioDTO crearUsuario(UsuarioDTO usuario) throws Exception { Mac sha256_HMAC = Mac.getInstance("HmacSHA256"); String key = "123"; SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(), "HmacSHA256"); sha256_HMAC.init(secret_key);//ww w . jav a 2 s. co m System.out.println("TO String: " + usuario.toString()); String hash = Hex.encodeHexString(sha256_HMAC.doFinal(usuario.toString().getBytes())); System.out.println("CODIGO HASH: " + hash); System.out.println("CODIGO HASH JSON " + usuario.getHash()); boolean alterado = !(hash.equalsIgnoreCase(usuario.getHash())); System.out.println("Alterado: " + alterado); if (alterado) { throw new Exception("Se han alterado los datos"); } return createUsuario(usuario); }
From source file:com.sshtools.j2ssh.transport.hmac.HmacSha.java
/** * * * @param keydata/*from w ww . j a v a 2s .c o m*/ * * @throws AlgorithmInitializationException */ public void init(byte[] keydata) throws AlgorithmInitializationException { try { mac = Mac.getInstance("HmacSha1"); byte[] key = new byte[20]; System.arraycopy(keydata, 0, key, 0, 20); SecretKeySpec keyspec = new SecretKeySpec(key, "HmacSha1"); mac.init(keyspec); } catch (NoSuchAlgorithmException nsae) { throw new AlgorithmInitializationException("No provider exists for the HmacSha1 algorithm"); } catch (InvalidKeyException ike) { throw new AlgorithmInitializationException("Invalid key"); } }
From source file:fitmon.WorkoutApi.java
public StringBuilder apiGetInfo() throws ClientProtocolException, IOException, NoSuchAlgorithmException, InvalidKeyException { HttpClient client = new DefaultHttpClient(); //HttpGet request = new HttpGet("http://platform.fatsecret.com/rest/server.api&" // + "oauth_consumer_key=5f2d9f7c250c4d75b9807a4f969363a7&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1408230438&" // + "oauth_nonce=abc&oauth_version=1.0&method=food.get&food_id=4384"); //HttpResponse response = client.execute(request); //BufferedReader rd = new BufferedReader (new InputStreamReader(response.getEntity().getContent())); //StringBuilder sb = new StringBuilder(); String base = URLEncoder.encode("GET") + "&"; base += "http%3A%2F%2Fplatform.fatsecret.com%2Frest%2Fserver.api&"; String params;// w ww .j a v a 2 s . co m params = "format=json&"; params += "method=exercises.get&"; params += "oauth_consumer_key=5f2d9f7c250c4d75b9807a4f969363a7&"; // ur consumer key params += "oauth_nonce=123&"; params += "oauth_signature_method=HMAC-SHA1&"; Date date = new java.util.Date(); Timestamp ts = new Timestamp(date.getTime()); params += "oauth_timestamp=" + ts.getTime() + "&"; params += "oauth_version=1.0"; //params += "search_expression=apple"; String params2 = URLEncoder.encode(params); base += params2; //System.out.println(base); String line = ""; String secret = "76172de2330a4e55b90cbd2eb44f8c63&"; Mac sha256_HMAC = Mac.getInstance("HMACSHA1"); SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HMACSHA1"); sha256_HMAC.init(secret_key); String hash = Base64.encodeBase64String(sha256_HMAC.doFinal(base.getBytes())); //$url = "http://platform.fatsecret.com/rest/server.api?".$params."&oauth_signature=".rawurlencode($sig); HttpGet request = new HttpGet("http://platform.fatsecret.com/rest/server.api?" + params + "&oauth_signature=" + URLEncoder.encode(hash)); HttpResponse response = client.execute(request); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuilder sb = new StringBuilder(); //$url = "http://platform.fatsecret.com/rest/server.api?"+params+"&oauth_signature="+URLEncoder.encode(hash); //return url; while ((line = rd.readLine()) != null) { sb.append(line); //System.out.println(line); } //System.out.println(sb.toString()); return sb; }
From source file:illab.nabal.util.SecurityHelper.java
/** * Get HMACSHA1-encoded OAuth 1.0a signature string. * /*from www.j a v a2s.c om*/ * @param secretKey - secret key to encode basestring with * @param baseString - signature base string * @return oauthSignature - HMAC-SHA1 encoded signature string * @throws Exception */ public static String getHmacSha1Signature(String secretKey, String baseString) throws Exception { String oauthSignature = null; // #################### IMPORTANT #################### // the secret key is the concatenated values (each first encoded per Parameter // Encoding) of the Consumer Secret and Token Secret, separated by an '&' character // (ASCII code 38) even if empty. if (StringHelper.isAllFull(secretKey, baseString) == true) { byte[] keyBytes = secretKey.getBytes(HTTP.UTF_8); SecretKey keySpec = new SecretKeySpec(keyBytes, HMAC_SHA1); Mac mac = Mac.getInstance(HMAC_SHA1); mac.init(keySpec); oauthSignature = new String(Base64.encode(mac.doFinal(baseString.getBytes(HTTP.UTF_8)), Base64.DEFAULT), HTTP.UTF_8).trim(); } return oauthSignature; }
From source file:chronic.app.ChronicCookie.java
public static String createAuthCode(byte[] secret, String string, long value) throws GeneralSecurityException { Mac mac = Mac.getInstance("HmacSHA1"); SecretKeySpec signKey = new SecretKeySpec(secret, "HmacSHA1"); mac.init(signKey);/*from ww w . j a va2 s. com*/ mac.update(string.getBytes()); return new Base32().encodeAsString(mac.doFinal(Bytes.toByteArray(value))); }
From source file:com.kku.apps.pricesearch.util.SignedHelper.java
private static String sign(String apiSecretKey, String sourceString) throws Exception { SecretKeySpec secretKeySpec = new SecretKeySpec(apiSecretKey.getBytes(ENCODING), ALGORITHM); Mac mac = Mac.getInstance(ALGORITHM); mac.init(secretKeySpec);/* ww w. j a v a 2s .c o m*/ byte[] data = mac.doFinal(sourceString.getBytes(ENCODING)); Base64 encoder = new Base64(); String signature = new String(encoder.encode(data)); return signature; }
From source file:edu.wfu.inotado.helper.EncryptionHelper.java
public String calculateHMAC(String data, String key) throws java.security.SignatureException { String result = ""; try {// www .j a v a 2 s.c o m if (!StringUtils.isBlank(data) && !StringUtils.isBlank(key)) { // 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(data.getBytes()); // base64-encode the hmac result = new String(Base64.encodeBase64(rawHmac)); } else { log.warn("data or key appears to be empty!"); } } catch (Exception e) { throw new SignatureException("Failed to generate HMAC : " + e.getMessage()); } return result; }
From source file:com.opentok.test.Helpers.java
private static String signData(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);//from w w w. j av a 2 s . co m return toHexString(mac.doFinal(data.getBytes())); }
From source file:SecurityUtils.java
/** * Converts a source string to its HMAC/SHA-1 value. * //w ww . j a v a2 s .com * @param source * The source string to convert. * @param secretKey * The secret key to use for conversion. * @return The HMac value of the source string. */ public static byte[] toHMac(String source, String secretKey) { byte[] result = null; try { // Create the HMAC/SHA1 key final SecretKeySpec signingKey = new SecretKeySpec(secretKey.getBytes(), "HmacSHA1"); // Create the message authentication code (MAC) final Mac mac = Mac.getInstance("HmacSHA1"); mac.init(signingKey); // Compute the HMAC value result = mac.doFinal(source.getBytes()); } catch (NoSuchAlgorithmException nsae) { throw new RuntimeException("Could not find the SHA-1 algorithm. HMac conversion failed.", nsae); } catch (InvalidKeyException ike) { throw new RuntimeException("Invalid key exception detected. HMac conversion failed.", ike); } return result; }
From source file:net.sourceforge.js3tream.util.Access.java
/******************************************************* * @throws Exception/*from ww w. jav a2 s . com*/ ******************************************************/ public Access() throws Exception { this.calFormat_ = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US); this.calFormat_.setTimeZone(TimeZone.getTimeZone("GMT")); this.mac_ = Mac.getInstance(HMAC_SHA1); this.mac_.init(new SecretKeySpec(Access.accessSecret_.getBytes(), HMAC_SHA1)); }