All Downloads are FREE. Search and download functionalities are using the official Maven repository.

common.crypto.HMAC Maven / Gradle / Ivy

Go to download

This is a collection of JAVA libraries that implement Unbound cryptographic classes for JAVA provider, PKCS11 wrapper, cryptoki, and advapi

There is a newer version: 42761
Show newest version
package com.unbound.common.crypto;

import com.unbound.common.STR;

import javax.crypto.Mac;

public class HMAC
{
  private Mac mac;

  protected HMAC(String type, byte[] key)
  {
    mac = SystemProvider.Mac.getInstance(type);
  }

  public byte[] end()
  {
    return mac.doFinal();
  }

  public HMAC update(byte[] in)
  {
    mac.update(in);
    return this;
  }

  public HMAC update(String in)
  {
    mac.update(STR.utf8(in));
    return this;
  }


  public static final class SHA256 extends HMAC
  {
    public SHA256(byte[] key)
    {
      super("HmacSHA256", key);
    }

    public static byte[] hmac(byte[] key, byte[] in)
    {
      return new SHA256(key).update(in).end();
    }

    public static byte[] hmac(byte[] key, String in)
    {
      return new SHA256(key).update(in).end();
    }
  }

  public static final class SHA512 extends HMAC
  {
    public SHA512(byte[] key)
    {
      super("HmacSHA512", key);
    }

    public static byte[] hmac(byte[] key, byte[] in)
    {
      return new SHA512(key).update(in).end();
    }

    public static byte[] hmac(byte[] key, String in)
    {
      return new SHA512(key).update(in).end();
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy