List of usage examples for javax.crypto Mac doFinal
public final byte[] doFinal() throws IllegalStateException
From source file:com.alibaba.openapi.client.util.SignatureUtil.java
public static byte[] hmacSha1(String path, List<NameValuePair> parameters, SecretKeySpec signingKey) { Mac mac; try {/*w w w.jav a2s. c o m*/ mac = Mac.getInstance(HMAC_SHA1); mac.init(signingKey); } catch (NoSuchAlgorithmException e) { throw new IllegalStateException(e.getMessage(), e); } catch (InvalidKeyException e) { throw new IllegalStateException(e.getMessage(), e); } mac.update(path.getBytes(CHARSET_UTF8)); Collections.sort(parameters, new NameValuePairComparator<NameValuePair>()); for (NameValuePair parameter : parameters) { mac.update(parameter.getName().getBytes(CHARSET_UTF8)); mac.update(parameter.getValue().getBytes(CHARSET_UTF8)); } return mac.doFinal(); }
From source file:lucee.commons.io.res.type.s3.S3.java
private static byte[] HMAC_SHA1(String key, String message, String charset) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException { SecretKeySpec sks = new SecretKeySpec(key.getBytes(charset), "HmacSHA1"); Mac mac = Mac.getInstance(sks.getAlgorithm()); mac.init(sks);/*from w w w. ja v a 2 s . co m*/ mac.update(message.getBytes(charset)); return mac.doFinal(); }
From source file:org.glite.slcs.caclient.impl.CMPRequest.java
private static byte[] makeProtection(String secret, int iterCount, String owfAlgId, String macAlgId, DEROctetString salt, PKIMessage message) { byte[] saltBytes = salt.getOctets(); byte[] sharedSecret = secret.getBytes(); byte[] firstKey = new byte[sharedSecret.length + saltBytes.length]; for (int i = 0; i < sharedSecret.length; i++) { firstKey[i] = sharedSecret[i];//from www . j av a2 s . com } for (int i = 0; i < saltBytes.length; i++) { firstKey[sharedSecret.length + i] = saltBytes[i]; } // Construct the base key according to rfc4210, section 5.1.3.1 MessageDigest dig = null; Mac mac = null; try { dig = MessageDigest.getInstance(owfAlgId, "BC"); for (int i = 0; i < iterCount; i++) { firstKey = dig.digest(firstKey); dig.reset(); } mac = Mac.getInstance(macAlgId, "BC"); SecretKey key = new SecretKeySpec(firstKey, macAlgId); mac.init(key); } catch (Exception e) { log.error("Error while calculating PKIMessage protection", e); } mac.reset(); byte[] protectedBytes = message.getProtectedBytes(); mac.update(protectedBytes, 0, protectedBytes.length); return mac.doFinal(); }
From source file:org.ejbca.core.protocol.cmp.CmpMessageHelper.java
public static byte[] protectPKIMessageWithPBE(PKIMessage msg, String keyId, String raSecret, String digestAlgId, String macAlgId, int iterationCount) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException { if (LOG.isTraceEnabled()) { LOG.trace(">protectPKIMessageWithPBE()"); }// w w w.j a v a 2 s . c om // Create the PasswordBased protection of the message PKIHeaderBuilder head = getHeaderBuilder(msg.getHeader()); byte[] keyIdBytes; try { keyIdBytes = keyId.getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { keyIdBytes = keyId.getBytes(); LOG.info("UTF-8 not available, using platform default encoding for keyIdBytes."); } head.setSenderKID(new DEROctetString(keyIdBytes)); // SHA1 AlgorithmIdentifier owfAlg = new AlgorithmIdentifier(digestAlgId); // iterations, usually something like 1024 ASN1Integer iteration = new ASN1Integer(iterationCount); // HMAC/SHA1 AlgorithmIdentifier macAlg = new AlgorithmIdentifier(macAlgId); // We need some random bytes for the nonce byte[] saltbytes = createSenderNonce(); DEROctetString derSalt = new DEROctetString(saltbytes); // Create the new protected return message //String objectId = "1.2.840.113533.7.66.13" = passwordBasedMac; String objectId = CMPObjectIdentifiers.passwordBasedMac.getId(); PBMParameter pp = new PBMParameter(derSalt, owfAlg, iteration, macAlg); AlgorithmIdentifier pAlg = new AlgorithmIdentifier(new ASN1ObjectIdentifier(objectId), pp); head.setProtectionAlg(pAlg); // Calculate the protection bits byte[] rasecret = raSecret.getBytes(); byte[] basekey = new byte[rasecret.length + saltbytes.length]; System.arraycopy(rasecret, 0, basekey, 0, rasecret.length); System.arraycopy(saltbytes, 0, basekey, rasecret.length, saltbytes.length); // Construct the base key according to rfc4210, section 5.1.3.1 MessageDigest dig = MessageDigest.getInstance(owfAlg.getAlgorithm().getId(), "BC"); for (int i = 0; i < iterationCount; i++) { basekey = dig.digest(basekey); dig.reset(); } PKIHeader pkiHeader = head.build(); // Do the mac String macOid = macAlg.getAlgorithm().getId(); byte[] protectedBytes = CmpMessageHelper.getProtectedBytes(pkiHeader, msg.getBody()); //ret.getProtectedBytes(); Mac mac = Mac.getInstance(macOid, "BC"); SecretKey key = new SecretKeySpec(basekey, macOid); mac.init(key); mac.reset(); mac.update(protectedBytes, 0, protectedBytes.length); byte[] out = mac.doFinal(); DERBitString bs = new DERBitString(out); if (LOG.isTraceEnabled()) { LOG.trace("<protectPKIMessageWithPBE()"); } // Return response as byte array return CmpMessageHelper .pkiMessageToByteArray(new PKIMessage(pkiHeader, msg.getBody(), bs, msg.getExtraCerts())); }
From source file:com.cloud.servlet.ConsoleProxyServlet.java
public static String genAccessTicket(String host, String port, String sid, String tag, Date normalizedHashTime) { String params = "host=" + host + "&port=" + port + "&sid=" + sid + "&tag=" + tag; try {/*from w w w . ja va 2 s . c om*/ Mac mac = Mac.getInstance("HmacSHA1"); long ts = normalizedHashTime.getTime(); ts = ts / 60000; // round up to 1 minute String secretKey = _ms.getHashKey(); SecretKeySpec keySpec = new SecretKeySpec(secretKey.getBytes(), "HmacSHA1"); mac.init(keySpec); mac.update(params.getBytes()); mac.update(String.valueOf(ts).getBytes()); byte[] encryptedBytes = mac.doFinal(); return Base64.encodeBase64String(encryptedBytes); } catch (Exception e) { s_logger.error("Unexpected exception ", e); } return ""; }
From source file:org.apache.ws.security.message.token.UsernameToken.java
/** * P_hash as defined in RFC 2246 for TLS. * /*from w w w. java2s .co m*/ * @param secret is the key for the HMAC * @param seed the seed value to start the generation - A(0) * @param mac the HMAC algorithm * @param required number of bytes to generate * @return a byte array that contains a secret key * @throws Exception */ private static byte[] P_hash(byte[] secret, byte[] seed, Mac mac, int required) throws Exception { byte[] out = new byte[required]; int offset = 0, tocpy; byte[] A, tmp; // // A(0) is the seed // A = seed; SecretKeySpec key = new SecretKeySpec(secret, "HMACSHA1"); mac.init(key); while (required > 0) { mac.update(A); A = mac.doFinal(); mac.update(A); mac.update(seed); tmp = mac.doFinal(); tocpy = min(required, tmp.length); System.arraycopy(tmp, 0, out, offset, tocpy); offset += tocpy; required -= tocpy; } return out; }
From source file:com.moha.demo.utils.Hashsalt.java
public String encrypt(String password) { String algorithm = EnvUtils.getProperty("algorithm"); String keyString = EnvUtils.getProperty("keyString"); SecretKey key = new SecretKeySpec(keyString.getBytes(), algorithm); try {// ww w . j av a2 s.co m Mac m = Mac.getInstance(algorithm); m.init(key); m.update(password.getBytes()); byte[] mac = m.doFinal(); return toHexString(mac); } catch (Exception e) { System.out.println(e.toString()); } return StringUtils.EMPTY; }
From source file:com.twosigma.beakerx.security.HashedMessageAuthenticationCode.java
public String signBytes(List<byte[]> msg) { try {/*w w w . ja v a 2 s . c om*/ final Mac mac = Mac.getInstance(TYPE); mac.init(spec); msg.forEach(it -> mac.update(it)); byte[] digest = mac.doFinal(); return toHex(digest); } catch (InvalidKeyException e) { throw new RuntimeException(INVALID_HMAC_EXCEPTION, e); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } }
From source file:org.apache.abdera2.common.security.KeyBase.java
protected byte[] hmac(byte[]... mat) { try {/* w w w . j a v a2 s . c o m*/ Mac hmac = Mac.getInstance(alg); hmac.init(key); for (byte[] m : mat) hmac.update(m); return hmac.doFinal(); } catch (Throwable t) { throw new RuntimeException(t); } }
From source file:cl.whyem.testsutilityproject.otpgenerator.KeyBase.java
protected byte[] hmac(byte[]... mat) { try {//from ww w . j a va 2s . co m Mac hmac = Mac.getInstance(alg); hmac.init(key); for (byte[] m : mat) { hmac.update(m); } return hmac.doFinal(); } catch (Throwable t) { throw new RuntimeException(t); } }