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

com.unbound.provider.UBRSASignature 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.provider;

import com.unbound.client.*;
import com.unbound.common.crypto.SystemProvider;

import java.security.*;
import java.security.interfaces.RSAPublicKey;

public class UBRSASignature extends SignatureSpi
{
  private final SignatureOper oper = Client.getInstance().newSignatureOperation();

  UBRSASignature(HashType hash)
  {
    oper.mode = SignatureMode.RSA_PKCS1;
    oper.hashType = hash;
  }

  // ------------------------- interface ---------------------------------

  @Override
  protected void engineInitVerify(PublicKey publicKey) throws InvalidKeyException
  {
    if (!(publicKey instanceof RSAPublicKey)) throw new InvalidKeyException("Invalid key type");

    oper.reset();
    String hashName = oper.hashType==null ? "NONE" : oper.hashType.getName();
    oper.swSignature = SystemProvider.Signature.getInstance(hashName+"WithRSA");
    oper.swSignature.initVerify(publicKey);
  }

  @Override
  protected void engineInitSign(PrivateKey privateKey) throws InvalidKeyException
  {
    if (!(privateKey instanceof UBRSAPrivateKey)) throw new InvalidKeyException("Invalid key type");
    oper.reset();
    oper.keyObject = ((UBRSAPrivateKey)privateKey).object;
  }

  @Override
  protected void engineUpdate(byte b) throws SignatureException
  {
    oper.update(b);
  }

  @Override
  protected void engineUpdate(byte[] in, int inOffset, int inLen) throws SignatureException
  {
    oper.update(in, inOffset, inLen);
  }

  @Override
  protected byte[] engineSign() throws SignatureException
  {
    return oper.finalSign();
  }

  @Override
  protected boolean engineVerify(byte[] sigBytes) throws SignatureException
  {
    return oper.finalVerify(sigBytes);
  }

  @Override
  protected void engineSetParameter(String param, Object value) throws InvalidParameterException
  {
    throw new UnsupportedOperationException("setParameter() not supported");
  }

  @Override
  protected Object engineGetParameter(String param) throws InvalidParameterException
  {
    throw new UnsupportedOperationException("getParameter() not supported");
  }

  // ----------------------- sub-classes ----------------------
  public static final class NONE extends UBRSASignature
  { public NONE() { super(null); } }
  public static final class SHA1 extends UBRSASignature
  { public SHA1() { super(HashType.SHA1); } }
  public static final class SHA256 extends UBRSASignature
  { public SHA256() { super(HashType.SHA256); } }
  public static final class SHA384 extends UBRSASignature
  { public SHA384() { super(HashType.SHA384); } }
  public static final class SHA512 extends UBRSASignature
  { public SHA512() { super(HashType.SHA512); } }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy