Package javax.crypto

Examples of javax.crypto.Mac.doFinal()


   *//*
   +-------------------------------------------------------------------------*/
   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)));
   }

   /*----------------------------------------------------------------base64Url-+
   *//**
   *//*
 
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

  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;
  }

  private static MessageDigest getMessageDigest(String algorithm) {
    try {
View Full Code Here

        }
        // 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

        // Check the hash.
        Mac hmac = Mac.getInstance("HmacSHA256", hmacProvider);
        hmac.init(new SecretKeySpec(cryptoKey, "HmacSHA256"));
        hmac.update(plaintext);
        hmac.update(lengthBytes);
        byte[] hashCheck = hmac.doFinal();
        if(!Arrays.equals(hash, hashCheck)) {
          throw new CHKDecodeException("HMAC is wrong, wrong decryption key?");
        }
    } catch(GeneralSecurityException e) {
      throw new CHKDecodeException("Problem with JCA, should be impossible!", e);
View Full Code Here

        byte[] tmpLen = new byte[] {
              (byte)(dataLength >> 8), (byte)(dataLength & 0xff)
            };
        hmac.update(data);
        hmac.update(tmpLen);
        byte[] hash = hmac.doFinal();
        byte[] header = new byte[hash.length+2+2];
      if(blockHashAlgorithm == 0) cryptoAlgorithm = KeyBlock.HASH_SHA256;
      if(blockHashAlgorithm != KeyBlock.HASH_SHA256)
        throw new IllegalArgumentException("Unsupported block hash algorithm "+cryptoAlgorithm);
        header[0] = (byte)(blockHashAlgorithm >> 8);
View Full Code Here

        byte[] tmpLen = new byte[] {
              (byte)(dataLength >> 8), (byte)(dataLength & 0xff)
            };
        hmac.update(data);
        hmac.update(tmpLen);
        byte[] hash = hmac.doFinal();
        byte[] header = new byte[hash.length+2+2];
      if(blockHashAlgorithm == 0) cryptoAlgorithm = KeyBlock.HASH_SHA256;
      if(blockHashAlgorithm != KeyBlock.HASH_SHA256)
        throw new IllegalArgumentException("Unsupported block hash algorithm "+cryptoAlgorithm);
        header[0] = (byte)(blockHashAlgorithm >> 8);
View Full Code Here

        mac.reset();

        mac.update(message, 0, message.length);

        out = mac.doFinal();

        if (!areEqual(out, output))
        {
            fail("Failed - expected " + new String(Hex.encode(output)) + " got " + new String(Hex.encode(out)));
        }
View Full Code Here

        mac.init(kGen.generateKey());

        mac.update(message);

        out = mac.doFinal();
    }

    private void testExceptions()
        throws Exception
    {
View Full Code Here

        mac.update(input0, 0, input0.length);

        byte[] out = new byte[mac.getMacLength()];

        mac.doFinal(out, 0);

        if (!areEqual(out, output_k128_m0))
        {
            fail("Failed - expected " + new String(Hex.encode(output_k128_m0))
                + " got " + new String(Hex.encode(out)));
View Full Code Here

TOP
Copyright © 2018 www.massapi.com. 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.