Example usage for javax.crypto Mac update

List of usage examples for javax.crypto Mac update

Introduction

In this page you can find the example usage for javax.crypto Mac update.

Prototype

public final void update(ByteBuffer input) 

Source Link

Document

Processes input.remaining() bytes in the ByteBuffer input , starting at input.position() .

Usage

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    SecretKeySpec k = new SecretKeySpec("1234".getBytes(), "HMACSHA1");
    Mac m = Mac.getInstance("HmacMD5");
    m.init(k);//from ww  w.j ava2s  .  c om
    m.update("test".getBytes("UTF8"));
    byte s[] = m.doFinal();
    for (int i = 0; i < s.length; i++) {
        System.out.print(Integer.toHexString((0x000000ff & s[i]) | 0xffffff00).substring(6));
    }
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {

    SecureRandom random = new SecureRandom();
    byte[] keyBytes = new byte[20];
    random.nextBytes(keyBytes);/*w w  w .j  av a2  s  .c o  m*/
    SecretKeySpec key = new SecretKeySpec(keyBytes, "HMACSHA1");

    System.out.println("Key:" + new BASE64Encoder().encode(key.getEncoded()));

    Mac mac = Mac.getInstance("HmacSHA1");
    mac.init(key);

    mac.update("test".getBytes("UTF8"));
    byte[] result = mac.doFinal();

    System.out.println("MAC: " + new BASE64Encoder().encode(result));
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    String alg = "HmacMD5";
    Mac mac = Mac.getInstance(alg);
    KeyGenerator kg = KeyGenerator.getInstance(alg);
    SecretKey key = kg.generateKey();
    mac.init(key);// ww  w .j  a  v  a 2 s .  co  m
    mac.update("test".getBytes());
    byte[] b = mac.doFinal();
    System.out.println(new String(b));

}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

    SecureRandom random = new SecureRandom();
    IvParameterSpec ivSpec = createCtrIvForAES();
    Key key = createKeyForAES(256, random);
    Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding", "BC");
    String input = "12345678";
    Mac mac = Mac.getInstance("DES", "BC");
    byte[] macKeyBytes = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
    Key macKey = new SecretKeySpec(macKeyBytes, "DES");

    cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);

    byte[] cipherText = new byte[cipher.getOutputSize(input.length() + mac.getMacLength())];

    int ctLength = cipher.update(input.getBytes(), 0, input.length(), cipherText, 0);

    mac.init(macKey);/*from  www.  ja  v a  2  s .c  om*/
    mac.update(input.getBytes());

    ctLength += cipher.doFinal(mac.doFinal(), 0, mac.getMacLength(), cipherText, ctLength);

    cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);

    byte[] plainText = cipher.doFinal(cipherText, 0, ctLength);
    int messageLength = plainText.length - mac.getMacLength();

    mac.init(macKey);
    mac.update(plainText, 0, messageLength);

    byte[] messageHash = new byte[mac.getMacLength()];
    System.arraycopy(plainText, messageLength, messageHash, 0, messageHash.length);

    System.out.println("plain : " + new String(plainText) + " verified: "
            + MessageDigest.isEqual(mac.doFinal(), messageHash));

}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Security.addProvider(new BouncyCastleProvider());
    SecureRandom random = new SecureRandom();
    IvParameterSpec ivSpec = createCtrIvForAES(1, random);
    Key key = createKeyForAES(256, random);
    Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding", "BC");
    String input = "www.java2s.com";
    Mac mac = Mac.getInstance("DES", "BC");
    byte[] macKeyBytes = "12345678".getBytes();
    Key macKey = new SecretKeySpec(macKeyBytes, "DES");
    System.out.println("input : " + input);

    // encryption step
    cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);
    byte[] cipherText = new byte[cipher.getOutputSize(input.length() + mac.getMacLength())];
    int ctLength = cipher.update(input.getBytes(), 0, input.length(), cipherText, 0);
    mac.init(macKey);/* w  w w .  j a  v a2s.com*/
    mac.update(input.getBytes());
    ctLength += cipher.doFinal(mac.doFinal(), 0, mac.getMacLength(), cipherText, ctLength);
    System.out.println("cipherText : " + new String(cipherText));

    // decryption step
    cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);
    byte[] plainText = cipher.doFinal(cipherText, 0, ctLength);
    int messageLength = plainText.length - mac.getMacLength();

    mac.init(macKey);
    mac.update(plainText, 0, messageLength);

    byte[] messageHash = new byte[mac.getMacLength()];
    System.arraycopy(plainText, messageLength, messageHash, 0, messageHash.length);

    System.out.println("plain : " + new String(plainText) + " verified: "
            + MessageDigest.isEqual(mac.doFinal(), messageHash));
}

From source file:crocserver.app.CrocSecurity.java

public static String createCode(byte[] secret, String string, long value) throws Exception {
    Mac mac = createHmac(secret);
    mac.update(string.getBytes());
    return new Base32().encodeAsString(mac.doFinal(toByteArray(value)));
}

From source file:com.cloud.utils.EncryptionUtil.java

public static String generateSignature(String data, String key) {
    try {/* w ww .j av  a 2  s . 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: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  ava 2  s .c  o m
    mac.update(string.getBytes());
    return new Base32().encodeAsString(mac.doFinal(Bytes.toByteArray(value)));
}

From source file:com.cloud.sample.UserCloudAPIExecutor.java

/**
 * 1. Signs a string with a secret key using SHA-1 2. Base64 encode the result 3. URL encode the final result
 * /*from  w w  w.  ja v  a 2 s . c o m*/
 * @param request
 * @param key
 * @return
 */
public static String signRequest(String request, String key) {
    try {
        Mac mac = Mac.getInstance("HmacSHA1");
        SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "HmacSHA1");
        mac.init(keySpec);
        mac.update(request.getBytes());
        byte[] encryptedBytes = mac.doFinal();
        return URLEncoder.encode(Base64.encodeBase64String(encryptedBytes), "UTF-8");
    } catch (Exception ex) {
        System.out.println(ex);
    }
    return null;
}

From source file:org.identityconnectors.office365.jsontoken.JWTTokenHelper.java

/**
 * Sign the text with the symmetric key.
 * @param signingKey The Signing Key./*  ww  w  .j  ava 2 s .  co m*/
 * @param rawToken The rawToken that needs to be signed.
 * @return Signed byte array.
 * @throws SampleAppException
 */
private static byte[] signData(String signingKey, String rawToken) throws Exception {
    SecretKeySpec secretKey = null;

    secretKey = new SecretKeySpec(com.sun.org.apache.xerces.internal.impl.dv.util.Base64.decode(signingKey),
            "HmacSHA256");

    Mac mac;
    byte[] signedData = null;

    mac = Mac.getInstance("HmacSHA256");
    mac.init(secretKey);
    mac.update(rawToken.getBytes("UTF-8"));
    signedData = mac.doFinal();

    return signedData;
}