org.bouncycastle.pqc.crypto.xmss.DigestUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bcprov-ext-jdk15on Show documentation
Show all versions of bcprov-ext-jdk15on Show documentation
The Bouncy Castle Crypto package is a Java implementation of cryptographic algorithms. This jar contains JCE provider and lightweight API for the Bouncy Castle Cryptography APIs for JDK 1.5 to JDK 1.8. Note: this package includes the NTRU encryption algorithms.
package org.bouncycastle.pqc.crypto.xmss;
import java.util.HashMap;
import java.util.Map;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.nist.NISTObjectIdentifiers;
import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.Xof;
import org.bouncycastle.crypto.digests.SHA256Digest;
import org.bouncycastle.crypto.digests.SHA512Digest;
import org.bouncycastle.crypto.digests.SHAKEDigest;
class DigestUtil
{
private static Map nameToOid = new HashMap();
static
{
nameToOid.put("SHA-256", NISTObjectIdentifiers.id_sha256);
nameToOid.put("SHA-512", NISTObjectIdentifiers.id_sha512);
nameToOid.put("SHAKE128", NISTObjectIdentifiers.id_shake128);
nameToOid.put("SHAKE256", NISTObjectIdentifiers.id_shake256);
}
static Digest getDigest(ASN1ObjectIdentifier oid)
{
if (oid.equals(NISTObjectIdentifiers.id_sha256))
{
return new SHA256Digest();
}
if (oid.equals(NISTObjectIdentifiers.id_sha512))
{
return new SHA512Digest();
}
if (oid.equals(NISTObjectIdentifiers.id_shake128))
{
return new SHAKEDigest(128);
}
if (oid.equals(NISTObjectIdentifiers.id_shake256))
{
return new SHAKEDigest(256);
}
throw new IllegalArgumentException("unrecognized digest OID: " + oid);
}
static ASN1ObjectIdentifier getDigestOID(String name)
{
ASN1ObjectIdentifier oid = nameToOid.get(name);
if (oid != null)
{
return oid;
}
throw new IllegalArgumentException("unrecognized digest name: " + name);
}
public static int getDigestSize(Digest digest)
{
if (digest instanceof Xof)
{
return digest.getDigestSize() * 2;
}
return digest.getDigestSize();
}
}