Package javax.crypto

Examples of javax.crypto.Mac


     */
    private boolean verify(BigInteger slaveID, Proto.ClientMessageType msgType,
                           byte[] message)
    {
        // first, verify the signature matches the given slave ID
        Mac mac;
        synchronized (slaveIdToSecret) {
            mac = slaveIdToSecret.get(slaveID);
        }
        if (mac == null) {
            AppXrw.logger.warning("Rejected message from " + slaveID +
                                  ".  MAC not found.");
            return false;
        }
        mac.update(message, 0, message.length - 20);

        // copy the signature into its own array for processing
        byte[] signature = new byte[20];
        System.arraycopy(message, message.length - 20, signature, 0, 20);

        // compare the signatures
        if (!Arrays.equals(signature, mac.doFinal())) {
            AppXrw.logger.warning("Rejected message from " + slaveID +
                                  ".  Signature mismatch.");
            return false;
        }

View Full Code Here


     */
  public static String generateSignature(String data,String token)
    {
        byte[] byteHMAC = null;
        try {
            Mac mac = Mac.getInstance(HMAC_SHA1);
            SecretKeySpec spec;
            if(token == null)
            {
              String oauthSignature = encode(consumer_secret) + "&";
              spec = new SecretKeySpec(oauthSignature.getBytes(), HMAC_SHA1);
            }
            else
            {
              String oauthSignature = encode(consumer_secret) + "&" + encode(token);
              spec = new SecretKeySpec(oauthSignature.getBytes(), HMAC_SHA1);
            }
            mac.init(spec);
            byteHMAC = mac.doFinal(data.getBytes());
        } catch (InvalidKeyException e) {
            setLog(e.getMessage());
        } catch (NoSuchAlgorithmException ignore) {
            // should never happen
        }
View Full Code Here

        /**
         * Make sure that we can generate the  Mac.
         */
        try {
            Mac mac = Mac.getInstance(algorithm);
            mac.init(key);
        } catch (NoSuchAlgorithmException e) {
            assert false : "Should never have reached here";
        } catch (InvalidKeyException e) {
            assert false : "Should never have reached here";
        }
View Full Code Here

            bytes[i] = (byte) (n);
            n >>>= 8;
        }

        try {
            Mac mac = Mac.getInstance(algorithm);
            mac.init(key);
            mac.update(bytes);

            return mac.doFinal();
        } catch (NoSuchAlgorithmException e) {
        } catch (InvalidKeyException e) {
        }
        assert false : "Should never have reached here";
        return null;
View Full Code Here

            bytes[i] = (byte) (id);
            id >>>= 8;
        }

        try {
            Mac mac = Mac.getInstance(algorithm);
            mac.init(key);
            mac.update(bytes);

            return mac.doFinal();
        } catch (NoSuchAlgorithmException e) {
        } catch (InvalidKeyException e) {
        }
        assert false : "Should never have reached here";
        return null;
View Full Code Here

    private byte[] generateCramMD5HexClientResponse(String userName, String userPassword, byte[] challengeBytes) throws Exception
    {
        String macAlgorithm = "HmacMD5";
        byte[] digestedPasswordBytes = MessageDigest.getInstance("MD5").digest(userPassword.getBytes("UTF-8"));
        byte[] hexEncodedDigestedPasswordBytes = toHex(digestedPasswordBytes).getBytes("UTF-8");
        Mac mac = Mac.getInstance(macAlgorithm);
        mac.init(new SecretKeySpec(hexEncodedDigestedPasswordBytes, macAlgorithm));
        final byte[] messageAuthenticationCode = mac.doFinal(challengeBytes);
        String responseAsString = userName + " " + toHex(messageAuthenticationCode);
        return responseAsString.getBytes();
    }
View Full Code Here

    }

    private byte[] generateCramMD5ClientResponse(String userName, String userPassword, byte[] challengeBytes) throws Exception
    {
        String macAlgorithm = "HmacMD5";
        Mac mac = Mac.getInstance(macAlgorithm);
        mac.init(new SecretKeySpec(userPassword.getBytes("UTF-8"), macAlgorithm));
        final byte[] messageAuthenticationCode = mac.doFinal(challengeBytes);
        String responseAsString = userName + " " + toHex(messageAuthenticationCode);
        return responseAsString.getBytes();
    }
View Full Code Here

        implements DerivationAlgorithm {

    public byte[] createKey(byte[] secret, byte[] seed, int offset,
            long length) throws ConversationException {
        try {
            Mac mac = Mac.getInstance("HmacSHA1");

            byte[] tempBytes = P_hash(secret, seed, mac,
                    (offset + (int) length));

            byte[] key = new byte[(int) length];
View Full Code Here

        /**
         * Make sure that we can generate the  Mac.
         */
        try {
            Mac mac = Mac.getInstance(algorithm);
            mac.init(key);
        } catch (NoSuchAlgorithmException e) {
            assert false : "Should never have reached here";
            throw new ProviderException("No such algorithm: " + algorithm + ".  This can be caused by a misconfigured java.ext.dirs, JAVA_HOME or JRE_HOME environment variable.");
        } catch (InvalidKeyException e) {
            assert false : "Should never have reached here";
View Full Code Here

        for (int i = 7; i >= 0; i--) {
            bytes[i] = (byte) (n);
            n >>>= 8;
        }

        Mac mac = Mac.getInstance(algorithm);
        mac.init(key);
        mac.update(bytes);

        return mac.doFinal();
    }
View Full Code Here

TOP

Related Classes of javax.crypto.Mac

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.