com.unbound.client.SignatureOper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of unbound-java-provider Show documentation
Show all versions of unbound-java-provider Show documentation
This is a collection of JAVA libraries that implement Unbound cryptographic classes for JAVA provider, PKCS11 wrapper, cryptoki, and advapi
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(); }
}
}