Search in sources :

Example 1 with MacOutputStream

use of com.github.zhenwei.core.crypto.io.MacOutputStream in project LinLong-Java by zhenwei1108.

the class PKCS12PBEUtils method createMacCalculator.

static MacCalculator createMacCalculator(final ASN1ObjectIdentifier digestAlgorithm, ExtendedDigest digest, final PKCS12PBEParams pbeParams, final char[] password) {
    PKCS12ParametersGenerator pGen = new PKCS12ParametersGenerator(digest);
    pGen.init(PKCS12ParametersGenerator.PKCS12PasswordToBytes(password), pbeParams.getIV(), pbeParams.getIterations().intValue());
    final KeyParameter keyParam = (KeyParameter) pGen.generateDerivedMacParameters(digest.getDigestSize() * 8);
    final HMac hMac = new HMac(digest);
    hMac.init(keyParam);
    return new MacCalculator() {

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

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

        public byte[] getMac() {
            byte[] res = new byte[hMac.getMacSize()];
            hMac.doFinal(res, 0);
            return res;
        }

        public GenericKey getKey() {
            return new GenericKey(getAlgorithmIdentifier(), PKCS12ParametersGenerator.PKCS12PasswordToBytes(password));
        }
    };
}
Also used : PKCS12ParametersGenerator(com.github.zhenwei.core.crypto.generators.PKCS12ParametersGenerator) HMac(com.github.zhenwei.core.crypto.macs.HMac) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter) MacOutputStream(com.github.zhenwei.core.crypto.io.MacOutputStream) GenericKey(com.github.zhenwei.pkix.operator.GenericKey) MacCalculator(com.github.zhenwei.pkix.operator.MacCalculator) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)

Example 2 with MacOutputStream

use of com.github.zhenwei.core.crypto.io.MacOutputStream in project LinLong-Java by zhenwei1108.

the class BcKeyStoreSpi method engineStore.

public void engineStore(OutputStream stream, char[] password) throws IOException {
    DataOutputStream dOut = new DataOutputStream(stream);
    byte[] salt = new byte[STORE_SALT_SIZE];
    int iterationCount = MIN_ITERATIONS + (random.nextInt() & 0x3ff);
    random.nextBytes(salt);
    dOut.writeInt(version);
    dOut.writeInt(salt.length);
    dOut.write(salt);
    dOut.writeInt(iterationCount);
    HMac hMac = new HMac(new SHA1Digest());
    MacOutputStream mOut = new MacOutputStream(hMac);
    PBEParametersGenerator pbeGen = new PKCS12ParametersGenerator(new SHA1Digest());
    byte[] passKey = PBEParametersGenerator.PKCS12PasswordToBytes(password);
    pbeGen.init(passKey, salt, iterationCount);
    if (version < 2) {
        hMac.init(pbeGen.generateDerivedMacParameters(hMac.getMacSize()));
    } else {
        hMac.init(pbeGen.generateDerivedMacParameters(hMac.getMacSize() * 8));
    }
    for (int i = 0; i != passKey.length; i++) {
        passKey[i] = 0;
    }
    saveStore(new TeeOutputStream(dOut, mOut));
    byte[] mac = new byte[hMac.getMacSize()];
    hMac.doFinal(mac, 0);
    dOut.write(mac);
    dOut.close();
}
Also used : TeeOutputStream(com.github.zhenwei.core.util.io.TeeOutputStream) PKCS12ParametersGenerator(com.github.zhenwei.core.crypto.generators.PKCS12ParametersGenerator) DataOutputStream(java.io.DataOutputStream) HMac(com.github.zhenwei.core.crypto.macs.HMac) SHA1Digest(com.github.zhenwei.core.crypto.digests.SHA1Digest) MacOutputStream(com.github.zhenwei.core.crypto.io.MacOutputStream) PBEParametersGenerator(com.github.zhenwei.core.crypto.PBEParametersGenerator)

Aggregations

PKCS12ParametersGenerator (com.github.zhenwei.core.crypto.generators.PKCS12ParametersGenerator)2 MacOutputStream (com.github.zhenwei.core.crypto.io.MacOutputStream)2 HMac (com.github.zhenwei.core.crypto.macs.HMac)2 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)1 PBEParametersGenerator (com.github.zhenwei.core.crypto.PBEParametersGenerator)1 SHA1Digest (com.github.zhenwei.core.crypto.digests.SHA1Digest)1 KeyParameter (com.github.zhenwei.core.crypto.params.KeyParameter)1 TeeOutputStream (com.github.zhenwei.core.util.io.TeeOutputStream)1 GenericKey (com.github.zhenwei.pkix.operator.GenericKey)1 MacCalculator (com.github.zhenwei.pkix.operator.MacCalculator)1 DataOutputStream (java.io.DataOutputStream)1