Search in sources :

Example 1 with PKCS12MacCalculatorBuilder

use of com.github.zhenwei.pkix.pkcs.PKCS12MacCalculatorBuilder in project LinLong-Java by zhenwei1108.

the class JcePKCS12MacCalculatorBuilderProvider method get.

public PKCS12MacCalculatorBuilder get(final AlgorithmIdentifier algorithmIdentifier) {
    return new PKCS12MacCalculatorBuilder() {

        public MacCalculator build(final char[] password) throws OperatorCreationException {
            final PKCS12PBEParams pbeParams = PKCS12PBEParams.getInstance(algorithmIdentifier.getParameters());
            try {
                final ASN1ObjectIdentifier algorithm = algorithmIdentifier.getAlgorithm();
                final Mac mac = helper.createMac(algorithm.getId());
                PBEParameterSpec defParams = new PBEParameterSpec(pbeParams.getIV(), pbeParams.getIterations().intValue());
                final SecretKey key = new PKCS12Key(password);
                mac.init(key, defParams);
                return new MacCalculator() {

                    public AlgorithmIdentifier getAlgorithmIdentifier() {
                        return new AlgorithmIdentifier(algorithm, pbeParams);
                    }

                    public OutputStream getOutputStream() {
                        return new MacOutputStream(mac);
                    }

                    public byte[] getMac() {
                        return mac.doFinal();
                    }

                    public GenericKey getKey() {
                        return new GenericKey(getAlgorithmIdentifier(), key.getEncoded());
                    }
                };
            } catch (Exception e) {
                throw new OperatorCreationException("unable to create MAC calculator: " + e.getMessage(), e);
            }
        }

        public AlgorithmIdentifier getDigestAlgorithmIdentifier() {
            return new AlgorithmIdentifier(algorithmIdentifier.getAlgorithm(), DERNull.INSTANCE);
        }
    };
}
Also used : MacOutputStream(com.github.zhenwei.provider.jcajce.io.MacOutputStream) Mac(javax.crypto.Mac) MacCalculator(com.github.zhenwei.pkix.operator.MacCalculator) OperatorCreationException(com.github.zhenwei.pkix.operator.OperatorCreationException) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier) SecretKey(javax.crypto.SecretKey) PKCS12PBEParams(com.github.zhenwei.core.asn1.pkcs.PKCS12PBEParams) PKCS12MacCalculatorBuilder(com.github.zhenwei.pkix.pkcs.PKCS12MacCalculatorBuilder) PKCS12Key(com.github.zhenwei.provider.jcajce.PKCS12Key) GenericKey(com.github.zhenwei.pkix.operator.GenericKey) OperatorCreationException(com.github.zhenwei.pkix.operator.OperatorCreationException) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Aggregations

ASN1ObjectIdentifier (com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)1 PKCS12PBEParams (com.github.zhenwei.core.asn1.pkcs.PKCS12PBEParams)1 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)1 GenericKey (com.github.zhenwei.pkix.operator.GenericKey)1 MacCalculator (com.github.zhenwei.pkix.operator.MacCalculator)1 OperatorCreationException (com.github.zhenwei.pkix.operator.OperatorCreationException)1 PKCS12MacCalculatorBuilder (com.github.zhenwei.pkix.pkcs.PKCS12MacCalculatorBuilder)1 PKCS12Key (com.github.zhenwei.provider.jcajce.PKCS12Key)1 MacOutputStream (com.github.zhenwei.provider.jcajce.io.MacOutputStream)1 Mac (javax.crypto.Mac)1 SecretKey (javax.crypto.SecretKey)1 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)1