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

com.unbound.client.SignatureOper 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.client;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.ProviderException;
import java.security.Signature;
import java.security.SignatureException;

public abstract class SignatureOper extends CryptoOper
{
  private MessageDigest md = null;
  private ByteArrayOutputStream buffer = null;

  public Signature swSignature = null;
  public SignatureMode mode = null;
  public HashType hashType = null;
  public HashType mgfHashType = null;
  public int pssSaltLen = 0;

  public byte[] getBufferBytes()
  {
    if (buffer==null) return new byte[0];
    return buffer.toByteArray();
  }


  protected abstract byte[] hwSign(byte[] in);

  public void reset()
  {
    swSignature = null;
    buffer = null;
    md = null;
    super.reset();
  }

  private void checkSignInit()
  {
    if (hashType==null)
    {
      if (buffer==null) buffer = new ByteArrayOutputStream();
    }
    else
    {
      if (md==null) md = hashType.getMessageDigest();
    }
  }

  public void updateSign(byte[] in)
  {
    checkSignInit();
    if (hashType==null)
    {
      try { buffer.write(in); }
      catch (IOException e) { throw new ProviderException(e); }
    }
    else md.update(in);
  }

  public void updateSign(byte[] in, int offset, int length)
  {
    checkSignInit();
    if (hashType==null) buffer.write(in, offset, length);
    else md.update(in, offset, length);
  }

  public void updateSign(byte in)
  {
    checkSignInit();
    if (hashType==null) buffer.write(in);
    else md.update(in);
  }

  public byte[] finalSign()
  {
    checkSignInit();
    byte[] hash = hashType==null ? buffer.toByteArray() : md.digest();

    checkSession();
    try { return hwSign(hash); }
    finally { reset(); }
  }

  public byte[] sign(byte[] in)
  {
    updateSign(in);
    return finalSign();
  }

  public void updateVerify(byte[] in) throws SignatureException
  {
    if (swSignature==null) throw new ProviderException("Operation is not supported");
    swSignature.update(in);
  }

  public void update(byte in) throws SignatureException
  {
    if (swSignature!=null) swSignature.update(in);
    else updateSign(in);
  }

  public void update(byte[] in, int offset, int length) throws SignatureException
  {
    if (swSignature!=null) swSignature.update(in, offset, length);
    else updateSign(in, offset, length);
  }

  public boolean finalVerify(byte[] signature)
  {
    if (swSignature==null) throw new ProviderException("Operation is not supported");
    try
    {
      swSignature.verify(signature);
      return true;
    }
    catch (SignatureException e) { return false; }
    finally { reset(); }
  }

  public boolean verify(byte[] in, byte[] signature)
  {
    if (swSignature==null) throw new ProviderException("Operation is not supported");
    try
    {
      swSignature.update(in);
      swSignature.verify(signature);
      return true;
    }
    catch (SignatureException e) { return false; }
    finally { reset(); }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy