Package javax.crypto

Examples of javax.crypto.Mac


        assertEquals("f42bb0eeb018ebbd4597ae7213711ec60760843f", Hex.encodeHexString(mac.doFinal()));
    }

    @Test
    public void testInitializedMac() throws IOException {
        final Mac md5Mac = HmacUtils.getInitializedMac(HmacAlgorithms.HMAC_MD5, STANDARD_KEY_BYTES);
        final Mac md5Mac2 = HmacUtils.getInitializedMac("HmacMD5", STANDARD_KEY_BYTES);
        Assert.assertArrayEquals(STANDARD_MD5_RESULT_BYTES, HmacUtils.updateHmac(md5Mac, STANDARD_PHRASE_STRING)
                .doFinal());
        Assert.assertArrayEquals(STANDARD_MD5_RESULT_BYTES, HmacUtils.updateHmac(md5Mac2, STANDARD_PHRASE_STRING)
                .doFinal());
    }
View Full Code Here


   /*----------------------------------------------------------------base64Url-+
   *//**
   *//*
   +-------------------------------------------------------------------------*/
   static private String hashKey(String in) throws Exception {
      Mac mac = Mac.getInstance(ALGO);
      mac.init(new SecretKeySpec(paySecret.getBytes(ENC), ALGO));
      return base64Url(mac.doFinal(in.getBytes(ENC)));
   }
View Full Code Here

  }

  private String sign(String data, String key) {
    try {
      SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(ENCODING), SIGNATURE_METHOD);
      Mac mac = Mac.getInstance(SIGNATURE_METHOD);
      mac.init(signingKey);
      byte[] rawHmac = mac.doFinal(data.getBytes(ENCODING));
      return Base64.encodeBytes(rawHmac);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
View Full Code Here

    byte[] hash = digest.digest(data[data.length - 1]);
    return new Sha256Hash(hash);
  }

  public static Mac buildHmacSha1(SecretKey key) {
    Mac mac = getHmacSha1();
    try {
      mac.init(key);
    } catch (InvalidKeyException e) {
      throw new IllegalStateException("Unexpected encryption error", e);
    }
    return mac;
  }
View Full Code Here

    }
    return mac;
  }

  public static Mac buildHmacSha256(SecretKey key) {
    Mac mac = getHmacSha256();
    try {
      mac.init(key);
    } catch (InvalidKeyException e) {
      throw new IllegalStateException("Unexpected encryption error", e);
    }
    return mac;
  }
View Full Code Here

    SecretKeySpec signingKey = new SecretKeySpec(keyData, HMAC_SHA256_ALGORITHM);
    return signingKey;
  }

  public static byte[] hmacSha1(SecretKeySpec signingKey, byte[]... data) {
    Mac mac = buildHmacSha1(signingKey);

    return computeMac(mac, data);
  }
View Full Code Here

    return computeMac(mac, data);
  }

  public static byte[] hmacSha256(SecretKeySpec signingKey, byte[]... data) {
    Mac mac = buildHmacSha256(signingKey);

    return computeMac(mac, data);
  }
View Full Code Here

    byte[] hash = mac.doFinal(data[data.length - 1]);
    return hash;
  }

  public static byte[] hmacSha1(SecretKeySpec signingKey, byte[] buffer, int offset, int length) {
    Mac mac = buildHmacSha1(signingKey);

    mac.update(buffer, offset, length);
    byte[] hash = mac.doFinal();
    return hash;
  }
View Full Code Here

    }
  }

  private static Mac getMac(String algorithm) {
    try {
      Mac digest = Mac.getInstance(algorithm);
      return digest;
    } catch (NoSuchAlgorithmException e) {
      // should not happen
      throw new IllegalStateException("Could not find Mac algorithm: " + algorithm, e);
    }
View Full Code Here

        int size = ((plaintext[data.length] & 0xff) << 8) + (plaintext[data.length + 1] & 0xff);
        if((size > 32768) || (size < 0)) {
            throw new CHKDecodeException("Invalid size: "+size);
        }
        // Check the hash.
        Mac hmac = Mac.getInstance("HmacSHA256", hmacProvider);
        hmac.init(new SecretKeySpec(cryptoKey, "HmacSHA256"));
        hmac.update(plaintext); // plaintext includes lengthBytes
        byte[] hashCheck = hmac.doFinal();
        if(!Arrays.equals(hash, hashCheck)) {
          throw new CHKDecodeException("HMAC is wrong, wrong decryption key?");
        }
        return Key.decompress(dontCompress ? false : key.isCompressed(), plaintext, size, bf,
            Math.min(maxLength, CHKBlock.MAX_LENGTH_BEFORE_COMPRESSION), key.compressionAlgorithm, false);
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.