Example usage for org.bouncycastle.jcajce.provider.util DigestFactory getDigest

List of usage examples for org.bouncycastle.jcajce.provider.util DigestFactory getDigest

Introduction

In this page you can find the example usage for org.bouncycastle.jcajce.provider.util DigestFactory getDigest.

Prototype

public static Digest getDigest(String digestName) 

Source Link

Usage

From source file:com.joyent.http.signature.crypto.NativeRSAWithSHA.java

License:Open Source License

/**
 * Finds checksum by name./*from   ww  w .ja  v  a 2 s  .c o  m*/
 *
 * @param digest digest name
 * @return digest instance
 */
private static Digest getDigest(final String digest) {
    try {
        return new JceDigest(digest);
    } catch (CryptoException e) {
        String noHyphen = digest.replaceFirst("-", "");
        return DigestFactory.getDigest(noHyphen);
    }
}

From source file:dorkbox.util.crypto.CryptoECC.java

License:Apache License

/**
 * The message will have the (digestName) hash calculated and used for the signature.
 * <p/>//  w  ww. j  ava 2s.  co m
 * The returned signature is the {r,s} signature array.
 */
public static BigInteger[] generateSignature(String digestName, ECPrivateKeyParameters privateKey,
        SecureRandom secureRandom, byte[] bytes) {

    Digest digest = DigestFactory.getDigest(digestName);

    byte[] checksum = new byte[digest.getDigestSize()];

    digest.update(bytes, 0, bytes.length);
    digest.doFinal(checksum, 0);

    return generateSignatureForHash(privateKey, secureRandom, checksum);
}

From source file:dorkbox.util.crypto.CryptoECC.java

License:Apache License

/**
 * The message will have the (digestName) hash calculated and used for the signature.
 *
 * @param signature/*from  w  w  w.j  a v  a  2  s .  c  o  m*/
 *                 is the {r,s} signature array.
 *
 * @return true if the signature is valid
 */
public static boolean verifySignature(String digestName, ECPublicKeyParameters publicKey, byte[] message,
        BigInteger[] signature) {

    Digest digest = DigestFactory.getDigest(digestName);

    byte[] checksum = new byte[digest.getDigestSize()];

    digest.update(message, 0, message.length);
    digest.doFinal(checksum, 0);

    return verifySignatureHash(publicKey, checksum, signature);
}

From source file:dorkbox.util.crypto.signers.BcECDSAContentSignerBuilder.java

License:Apache License

@Override
protected Signer createSigner(AlgorithmIdentifier sigAlgId, AlgorithmIdentifier digAlgId)
        throws OperatorCreationException {
    Digest digest = DigestFactory.getDigest(digAlgId.getAlgorithm().getId()); // SHA1, SHA512, etc

    return new DSADigestSigner(new ECDSASigner(), digest);
}

From source file:dorkbox.util.crypto.signers.BcECDSAContentVerifierProviderBuilder.java

License:Apache License

@Override
protected Signer createSigner(AlgorithmIdentifier sigAlgId) throws OperatorCreationException {
    AlgorithmIdentifier digAlgId = new DefaultDigestAlgorithmIdentifierFinder().find(sigAlgId);

    Digest digest = DigestFactory.getDigest(digAlgId.getAlgorithm().getId()); // 1.2.23.4.5.6, etc
    return new DSADigestSigner(new ECDSASigner(), digest);
}

From source file:org.xipki.commons.security.pkcs11.provider.P11RSAPSSSignatureSpi.java

License:Open Source License

protected P11RSAPSSSignatureSpi(final PSSParameterSpec baseParamSpec, final boolean isRaw) {
    this.originalSpec = baseParamSpec;
    this.paramSpec = (baseParamSpec == null) ? PSSParameterSpec.DEFAULT : baseParamSpec;
    this.mgfDigest = DigestFactory.getDigest(paramSpec.getDigestAlgorithm());
    this.saltLength = paramSpec.getSaltLength();
    this.trailer = getTrailer(paramSpec.getTrailerField());
    this.isRaw = isRaw;

    setupContentDigest();//from  www . j  a  va 2s.  co  m
}

From source file:org.xipki.commons.security.pkcs11.provider.P11RSAPSSSignatureSpi.java

License:Open Source License

@Override
protected void engineSetParameter(final AlgorithmParameterSpec params) throws InvalidParameterException {
    if (params instanceof PSSParameterSpec) {
        PSSParameterSpec newParamSpec = (PSSParameterSpec) params;

        if (originalSpec != null) {
            if (!DigestFactory.isSameDigest(originalSpec.getDigestAlgorithm(),
                    newParamSpec.getDigestAlgorithm())) {
                throw new InvalidParameterException(
                        "parameter must be using " + originalSpec.getDigestAlgorithm());
            }/*from   ww w  .java  2 s.  c om*/
        }
        if (!newParamSpec.getMGFAlgorithm().equalsIgnoreCase("MGF1")
                && !newParamSpec.getMGFAlgorithm().equals(PKCSObjectIdentifiers.id_mgf1.getId())) {
            throw new InvalidParameterException("unknown mask generation function specified");
        }

        if (!(newParamSpec.getMGFParameters() instanceof MGF1ParameterSpec)) {
            throw new InvalidParameterException("unkown MGF parameters");
        }

        MGF1ParameterSpec mgfParams = (MGF1ParameterSpec) newParamSpec.getMGFParameters();

        if (!DigestFactory.isSameDigest(mgfParams.getDigestAlgorithm(), newParamSpec.getDigestAlgorithm())) {
            throw new InvalidParameterException(
                    "digest algorithm for MGF should be the same as for PSS parameters.");
        }

        Digest newDigest = DigestFactory.getDigest(mgfParams.getDigestAlgorithm());

        if (newDigest == null) {
            throw new InvalidParameterException(
                    "no match on MGF digest algorithm: " + mgfParams.getDigestAlgorithm());
        }

        this.engineParams = null;
        this.paramSpec = newParamSpec;
        this.mgfDigest = newDigest;
        this.saltLength = paramSpec.getSaltLength();
        this.trailer = getTrailer(paramSpec.getTrailerField());

        setupContentDigest();
    } else {
        throw new InvalidParameterException("only PSSParameterSpec supported");
    }
}

From source file:org.xipki.security.provider.RSAPSSSignatureSpi.java

License:Open Source License

protected RSAPSSSignatureSpi(final PSSParameterSpec baseParamSpec, final boolean isRaw) {
    this.originalSpec = baseParamSpec;

    if (baseParamSpec == null) {
        this.paramSpec = PSSParameterSpec.DEFAULT;
    } else {/*from  www  . ja v a 2  s  .c  o m*/
        this.paramSpec = baseParamSpec;
    }

    this.mgfDigest = DigestFactory.getDigest(paramSpec.getDigestAlgorithm());
    this.saltLength = paramSpec.getSaltLength();
    this.trailer = getTrailer(paramSpec.getTrailerField());
    this.isRaw = isRaw;

    setupContentDigest();
}

From source file:org.xipki.security.provider.RSAPSSSignatureSpi.java

License:Open Source License

protected void engineSetParameter(final AlgorithmParameterSpec params) throws InvalidParameterException {
    if (params instanceof PSSParameterSpec) {
        PSSParameterSpec newParamSpec = (PSSParameterSpec) params;

        if (originalSpec != null) {
            if (DigestFactory.isSameDigest(originalSpec.getDigestAlgorithm(),
                    newParamSpec.getDigestAlgorithm()) == false) {
                throw new InvalidParameterException(
                        "parameter must be using " + originalSpec.getDigestAlgorithm());
            }/*from  ww w. j  av a2s. c om*/
        }
        if ((newParamSpec.getMGFAlgorithm().equalsIgnoreCase("MGF1") == false)
                && (newParamSpec.getMGFAlgorithm().equals(PKCSObjectIdentifiers.id_mgf1.getId()) == false)) {
            throw new InvalidParameterException("unknown mask generation function specified");
        }

        if ((newParamSpec.getMGFParameters() instanceof MGF1ParameterSpec) == false) {
            throw new InvalidParameterException("unkown MGF parameters");
        }

        MGF1ParameterSpec mgfParams = (MGF1ParameterSpec) newParamSpec.getMGFParameters();

        if (DigestFactory.isSameDigest(mgfParams.getDigestAlgorithm(),
                newParamSpec.getDigestAlgorithm()) == false) {
            throw new InvalidParameterException(
                    "digest algorithm for MGF should be the same as for PSS parameters.");
        }

        Digest newDigest = DigestFactory.getDigest(mgfParams.getDigestAlgorithm());

        if (newDigest == null) {
            throw new InvalidParameterException(
                    "no match on MGF digest algorithm: " + mgfParams.getDigestAlgorithm());
        }

        this.engineParams = null;
        this.paramSpec = newParamSpec;
        this.mgfDigest = newDigest;
        this.saltLength = paramSpec.getSaltLength();
        this.trailer = getTrailer(paramSpec.getTrailerField());

        setupContentDigest();
    } else {
        throw new InvalidParameterException("only PSSParameterSpec supported");
    }
}