List of usage examples for javax.crypto Mac getInstance
public static final Mac getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:Main.java
public static String getBytesMacEncrypt(byte[] bytes, SecretKey key) { String algorithm = key.getAlgorithm(); if (algorithm.equals(HmacMD5) || algorithm.equals(HmacSHA1) || algorithm.equals(HmacSHA256) || algorithm.equals(HmacSHA384) || algorithm.equals(HmacSHA512)) { Mac mac = null;/*from w w w. ja va2 s. c om*/ try { mac = Mac.getInstance(algorithm); mac.init(key); return bytes2String(mac.doFinal(bytes)); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } } return null; }
From source file:de.hybris.platform.acceleratorservices.payment.utils.impl.DefaultAcceleratorDigestUtils.java
@Override public String getPublicDigest(final String customValues, final String key) throws NoSuchAlgorithmException, InvalidKeyException { final Base64 encoder = new Base64(); final Mac sha1Mac = Mac.getInstance(getMacAlgorithm()); final SecretKeySpec publicKeySpec = new SecretKeySpec(key.getBytes(), getMacAlgorithm()); sha1Mac.init(publicKeySpec);// w ww.j ava 2 s. c om final byte[] publicBytes = sha1Mac.doFinal(customValues.getBytes()); final String publicDigest = new String(encoder.encode(publicBytes)); return publicDigest.replaceAll("\n", ""); }
From source file:com.predic8.membrane.core.interceptor.authentication.session.totp.OtpProvider.java
static Signer getSigningOracle(String secret) { try {/*from ww w.ja v a 2 s . co m*/ byte[] keyBytes = decodeKey(secret); final Mac mac = Mac.getInstance("HMACSHA1"); mac.init(new SecretKeySpec(keyBytes, "")); // Create a signer object out of the standard Java MAC // implementation. return new Signer() { @Override public byte[] sign(byte[] data) { return mac.doFinal(data); } }; } catch (NoSuchAlgorithmException error) { log.error("", error); } catch (InvalidKeyException error) { log.error("", error); } return null; }
From source file:com.ljt.openapi.demo.util.SignUtil.java
/** * ??// www. j ava 2s . 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:com.amazon.pay.impl.Util.java
/** * Helper method to calculate base64 encoded signature using specified secret key * *//* w ww . ja va 2s . c om*/ public static String getSignature(String stringToSign, String secretKey) throws IllegalStateException, InvalidKeyException, NoSuchAlgorithmException, UnsupportedEncodingException { Mac mac = Mac.getInstance("HmacSHA256"); mac.init(new SecretKeySpec(secretKey.getBytes("UTF-8"), "HmacSHA256")); byte[] signature = mac.doFinal(stringToSign.getBytes("UTF-8")); String signatureBase64 = new String(Base64.encodeBase64(signature), "UTF-8"); return signatureBase64; }
From source file:net.seleucus.wsp.crypto.FwknopSymmetricCrypto.java
public static String sign(byte[] auth_key, String message, byte hmac_type) throws NoSuchAlgorithmException, InvalidKeyException { // Check if hmac_type is valid if (hmac_type > 4 || hmac_type < 0) throw new IllegalArgumentException("Invalid digest type was specified"); // Create Mac instance Mac hmac;/* w ww .j av a 2 s . co m*/ hmac = Mac.getInstance(HMAC_ALGORITHMS[hmac_type]); // Create key SecretKeySpec hmac_key = new SecretKeySpec(auth_key, HMAC_ALGORITHMS[hmac_type]); // Init hmac object hmac.init(hmac_key); // Prepare enc_part to calculate HMAC byte[] msg_to_hmac = FWKNOP_ENCRYPTION_HEADER.concat(message).getBytes(); // Calculate HMAC and return return message.concat(Base64.encodeBase64String(hmac.doFinal(msg_to_hmac)).replace("=", "")); }
From source file:com.javaps.springboot.LicenseController.java
@RequestMapping(value = "/public/license", produces = "text/plain", method = RequestMethod.POST) public String licenseValidate(HttpServletRequest req, @RequestBody String license) throws Exception { String clientIp = req.getHeader("X-Forwarded-For"); //nginx???IP if (clientIp == null) clientIp = req.getRemoteAddr(); //????? //System.out.println("clientIp="+clientIp); SecretKeySpec signingKey = new SecretKeySpec(licenseSecretKey.getBytes(), "HmacSHA1"); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(signingKey);/*w w w . j a v a2s . c o m*/ byte[] rawHmac = mac.doFinal(clientIp.getBytes()); //System.out.println("license should be:"+Base64.encodeBase64String(rawHmac)); if (!license.equals(Base64.encodeBase64String(rawHmac))) throw new Exception(); return "OK"; }
From source file:com.cloud.utils.EncryptionUtil.java
public static String generateSignature(String data, String key) { try {/*from w w w .j a v a 2s. c o m*/ final Mac mac = Mac.getInstance("HmacSHA1"); final SecretKeySpec keySpec = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA1"); mac.init(keySpec); mac.update(data.getBytes("UTF-8")); final byte[] encryptedBytes = mac.doFinal(); return Base64.encodeBase64String(encryptedBytes); } catch (NoSuchAlgorithmException | InvalidKeyException | UnsupportedEncodingException e) { s_logger.error("exception occurred which encoding the data." + e.getMessage()); throw new CloudRuntimeException("unable to generate signature", e); } }
From source file:spring.travel.site.auth.Signer.java
public String sign(String data) throws AuthException { try {//from w w w . jav a2 s . co m SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), HMAC_SHA1_ALGORITHM); Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); mac.init(signingKey); byte[] raw = mac.doFinal(data.getBytes(StandardCharsets.UTF_8)); return toHex(raw); } catch (InvalidKeyException | NoSuchAlgorithmException e) { throw new AuthException("Failed signing data", e); } }
From source file:com.spectralogic.ds3client.utils.Signature.java
/** * Computes RFC 2104-compliant HMAC signature. * * @param data/*from w ww.ja va2s . c o m*/ * The data to be signed. * @param key * The signing key. * @return * The Base64-encoded RFC 2104-compliant HMAC signature. * @throws * java.security.SignatureException when signature generation fails */ public static String calculateRFC2104HMAC(final String data, final String key) throws java.security.SignatureException { LOG.debug("String to sign: {}", data.replace("\n", "\\n")); final String result; try { // get an hmac_sha1 key from the raw key bytes final SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(Charset.forName("UTF-8")), HMAC_SHA1_ALGORITHM); // get an hmac_sha1 Mac instance and initialize with the signing key final Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); mac.init(signingKey); // compute the hmac on input data bytes final byte[] rawHmac = mac.doFinal(data.getBytes(Charset.forName("UTF-8"))); result = Base64.encodeBase64String(rawHmac); } catch (final Exception e) { throw new SignatureException("Failed to generate HMAC : " + e.getMessage()); } return result.trim(); }