Module: PkernelJce::BcHelpers

Defined in:
lib/pkernel_jce/bc_helpers.rb

Overview

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

Class Method Summary collapse

Class Method Details

.find_digest_calculator(algo, prov = PkernelJce::Provider::DefProvider) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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