module PkernelJce::BcHelpers

BC has notorious habit of complicating the object space This attempt to create single API from all those BC intermidiary objects/classes

Public Class Methods

find_digest_calculator(algo, prov = PkernelJce::Provider::DefProvider) click to toggle source
# File lib/pkernel_jce/bc_helpers.rb, line 8
def BcHelpers.find_digest_calculator(algo, prov = PkernelJce::Provider::DefProvider)

  digestSel = org.bouncycastle.operator.jcajce.JcaDigestCalculatorProviderBuilder.new.setProvider(prov).build
  
  case algo
  when :sha256, "SHA256"
    digestSel.get(org.bouncycastle.asn1.x509.AlgorithmIdentifier.new(org.bouncycastle.asn1.nist.NISTObjectIdentifiers.id_sha256))
  when :sha384, "SHA384"
    digestSel.get(org.bouncycastle.asn1.x509.AlgorithmIdentifier.new(org.bouncycastle.asn1.nist.NISTObjectIdentifiers.id_sha384))
  when :sha512, "SHA512"
    digestSel.get(org.bouncycastle.asn1.x509.AlgorithmIdentifier.new(org.bouncycastle.asn1.nist.NISTObjectIdentifiers.id_sha512))
  when :sha512_224
    digestSel.get(org.bouncycastle.asn1.x509.AlgorithmIdentifier.new(org.bouncycastle.asn1.nist.NISTObjectIdentifiers.id_sha512_224))
  when :sha512_256
    digestSel.get(org.bouncycastle.asn1.x509.AlgorithmIdentifier.new(org.bouncycastle.asn1.nist.NISTObjectIdentifiers.id_sha512_256))
  when :sha3_224
    digestSel.get(org.bouncycastle.asn1.x509.AlgorithmIdentifier.new(org.bouncycastle.asn1.nist.NISTObjectIdentifiers.id_sha3_224))
  when :sha3_256
    digestSel.get(org.bouncycastle.asn1.x509.AlgorithmIdentifier.new(org.bouncycastle.asn1.nist.NISTObjectIdentifiers.id_sha3_256))
  when :sha3_384
    digestSel.get(org.bouncycastle.asn1.x509.AlgorithmIdentifier.new(org.bouncycastle.asn1.nist.NISTObjectIdentifiers.id_sha3_384))
  when :sha3_512
    digestSel.get(org.bouncycastle.asn1.x509.AlgorithmIdentifier.new(org.bouncycastle.asn1.nist.NISTObjectIdentifiers.id_sha3_512))
  when :shake128
    digestSel.get(org.bouncycastle.asn1.x509.AlgorithmIdentifier.new(org.bouncycastle.asn1.nist.NISTObjectIdentifiers.id_shake128))
  when :shake256
    digestSel.get(org.bouncycastle.asn1.x509.AlgorithmIdentifier.new(org.bouncycastle.asn1.nist.NISTObjectIdentifiers.id_shake256))
  when :ripemd128
    digestSel.get(org.bouncycastle.asn1.x509.AlgorithmIdentifier.new(org.bouncycastle.asn1.teletrust.TeleTrusTObjectIdentifiers.ripemd128))
  when :ripemd160
    digestSel.get(org.bouncycastle.asn1.x509.AlgorithmIdentifier.new(org.bouncycastle.asn1.teletrust.TeleTrusTObjectIdentifiers.ripemd160))
  when :ripemd256
    digestSel.get(org.bouncycastle.asn1.x509.AlgorithmIdentifier.new(org.bouncycastle.asn1.teletrust.TeleTrusTObjectIdentifiers.ripemd256))
  else
    dgstCal = digestSel.get(org.bouncycastle.asn1.x509.AlgorithmIdentifier.new(algo))
    if dgstCal.nil?
      raise PkernelJce::Error, "Unknown digest algo '#{algo}'"
    else
      dgstCal
    end
  end
end