Search in sources :

Example 1 with SHA1Digest

use of org.spongycastle.crypto.digests.SHA1Digest in project universa by UniversaBlockchain.

the class OAEPEncodingTest method maskGeneratorFunction1.

/**
 * Tests the private implementation of maskGeneratorFunction1 using reference vectors
 * from RSA OAEP spec.
 *
 * @throws Exception
 */
@Test
public void maskGeneratorFunction1() throws Exception {
    OAEPEncoding oaepEncoder = new OAEPEncoding(RSAEngineFactory.make(), new SHA1Digest());
    byte[] dbMask_test = MGF(oaepEncoder, oaepSpec.seed, 107);
    assertArrayEquals(dbMask_test, oaepSpec.dbMask);
    byte[] maskedDB_test = new byte[oaepSpec.DB.length];
    for (int i = 0; i < oaepSpec.DB.length; i++) {
        maskedDB_test[i] = (byte) (oaepSpec.DB[i] ^ oaepSpec.dbMask[i]);
    }
    assertArrayEquals(maskedDB_test, oaepSpec.maskedDB);
    byte[] seedMask_test = MGF(oaepEncoder, oaepSpec.maskedDB, 20);
    assertArrayEquals(seedMask_test, oaepSpec.seedMask);
    byte[] maskedSeed_test = new byte[oaepSpec.seed.length];
    for (int i = 0; i < oaepSpec.seed.length; i++) {
        maskedSeed_test[i] = (byte) (oaepSpec.seed[i] ^ oaepSpec.seedMask[i]);
    }
    assertArrayEquals(maskedSeed_test, oaepSpec.maskedSeed);
    ByteArrayOutputStream emStream = new ByteArrayOutputStream();
    emStream.write(oaepSpec.maskedSeed);
    emStream.write(oaepSpec.maskedDB);
    byte[] em_test = emStream.toByteArray();
    assertArrayEquals(em_test, oaepSpec.EM);
}
Also used : SHA1Digest(org.spongycastle.crypto.digests.SHA1Digest) OAEPEncoding(org.spongycastle.crypto.encodings.OAEPEncoding) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 2 with SHA1Digest

use of org.spongycastle.crypto.digests.SHA1Digest in project balzac by balzac-lang.

the class BitcoinUtils method sha1.

public static Hash sha1(byte[] bytes) {
    SHA1Digest digest = new SHA1Digest();
    digest.update(bytes, 0, bytes.length);
    byte[] sha1Hash = new byte[20];
    digest.doFinal(sha1Hash, 0);
    return new Hash(sha1Hash);
}
Also used : SHA1Digest(org.spongycastle.crypto.digests.SHA1Digest) Hash(it.unica.tcs.lib.Hash) Sha256Hash(org.bitcoinj.core.Sha256Hash)

Example 3 with SHA1Digest

use of org.spongycastle.crypto.digests.SHA1Digest in project rskj by rsksmart.

the class ECIESCoder method decryptSimple.

/**
 *  Encryption equivalent to the Crypto++ default ECIES<ECP> settings:
 *
 *  DL_KeyAgreementAlgorithm:        DL_KeyAgreementAlgorithm_DH<struct ECPPoint,struct EnumToType<enum CofactorMultiplicationOption,0> >
 *  DL_KeyDerivationAlgorithm:       DL_KeyDerivationAlgorithm_P1363<struct ECPPoint,0,class P1363_KDF2<class SHA1> >
 *  DL_SymmetricEncryptionAlgorithm: DL_EncryptionAlgorithm_Xor<class HMAC<class SHA1>,0>
 *  DL_PrivateKey:                   DL_Key<ECPPoint>
 *  DL_PrivateKey_EC<class ECP>
 *
 *  Used for Whisper V3
 */
public static byte[] decryptSimple(BigInteger privKey, byte[] cipher) throws IOException, InvalidCipherTextException {
    EthereumIESEngine iesEngine = new EthereumIESEngine(new ECDHBasicAgreement(), new MGF1BytesGeneratorExt(new SHA1Digest(), 1), new HMac(new SHA1Digest()), new SHA1Digest(), null);
    IESParameters p = new IESParameters(null, null, KEY_SIZE);
    ParametersWithIV parametersWithIV = new ParametersWithIV(p, new byte[0]);
    iesEngine.setHashMacKey(false);
    iesEngine.init(new ECPrivateKeyParameters(privKey, CURVE), parametersWithIV, new ECIESPublicKeyParser(ECKey.CURVE));
    return iesEngine.processBlock(cipher, 0, cipher.length);
}
Also used : ECDHBasicAgreement(org.spongycastle.crypto.agreement.ECDHBasicAgreement) HMac(org.spongycastle.crypto.macs.HMac) SHA1Digest(org.spongycastle.crypto.digests.SHA1Digest) ECIESPublicKeyParser(org.spongycastle.crypto.parsers.ECIESPublicKeyParser)

Example 4 with SHA1Digest

use of org.spongycastle.crypto.digests.SHA1Digest in project rskj by rsksmart.

the class ECIESCoder method encryptSimple.

/**
 *  Encryption equivalent to the Crypto++ default ECIES<ECP> settings:
 *
 *  DL_KeyAgreementAlgorithm:        DL_KeyAgreementAlgorithm_DH<struct ECPPoint,struct EnumToType<enum CofactorMultiplicationOption,0> >
 *  DL_KeyDerivationAlgorithm:       DL_KeyDerivationAlgorithm_P1363<struct ECPPoint,0,class P1363_KDF2<class SHA1> >
 *  DL_SymmetricEncryptionAlgorithm: DL_EncryptionAlgorithm_Xor<class HMAC<class SHA1>,0>
 *  DL_PrivateKey:                   DL_Key<ECPPoint>
 *  DL_PrivateKey_EC<class ECP>
 *
 *  Used for Whisper V3
 */
public static byte[] encryptSimple(ECPoint pub, byte[] plaintext) throws IOException, InvalidCipherTextException {
    EthereumIESEngine iesEngine = new EthereumIESEngine(new ECDHBasicAgreement(), new MGF1BytesGeneratorExt(new SHA1Digest(), 1), new HMac(new SHA1Digest()), new SHA1Digest(), null);
    IESParameters p = new IESParameters(null, null, KEY_SIZE);
    ParametersWithIV parametersWithIV = new ParametersWithIV(p, new byte[0]);
    iesEngine.setHashMacKey(false);
    ECKeyPairGenerator eGen = new ECKeyPairGenerator();
    SecureRandom random = new SecureRandom();
    KeyGenerationParameters gParam = new ECKeyGenerationParameters(CURVE, random);
    eGen.init(gParam);
    EphemeralKeyPairGenerator ephemeralKeyPairGenerator = new EphemeralKeyPairGenerator(/*testGen*/
    eGen, new ECIESPublicKeyEncoder());
    iesEngine.init(new ECPublicKeyParameters(pub, CURVE), parametersWithIV, ephemeralKeyPairGenerator);
    return iesEngine.processBlock(plaintext, 0, plaintext.length);
}
Also used : ECKeyPairGenerator(org.spongycastle.crypto.generators.ECKeyPairGenerator) EphemeralKeyPairGenerator(org.spongycastle.crypto.generators.EphemeralKeyPairGenerator) HMac(org.spongycastle.crypto.macs.HMac) SHA1Digest(org.spongycastle.crypto.digests.SHA1Digest) SecureRandom(java.security.SecureRandom) KeyGenerationParameters(org.spongycastle.crypto.KeyGenerationParameters) ECDHBasicAgreement(org.spongycastle.crypto.agreement.ECDHBasicAgreement)

Aggregations

SHA1Digest (org.spongycastle.crypto.digests.SHA1Digest)4 ECDHBasicAgreement (org.spongycastle.crypto.agreement.ECDHBasicAgreement)2 HMac (org.spongycastle.crypto.macs.HMac)2 Hash (it.unica.tcs.lib.Hash)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 SecureRandom (java.security.SecureRandom)1 Sha256Hash (org.bitcoinj.core.Sha256Hash)1 Test (org.junit.Test)1 KeyGenerationParameters (org.spongycastle.crypto.KeyGenerationParameters)1 OAEPEncoding (org.spongycastle.crypto.encodings.OAEPEncoding)1 ECKeyPairGenerator (org.spongycastle.crypto.generators.ECKeyPairGenerator)1 EphemeralKeyPairGenerator (org.spongycastle.crypto.generators.EphemeralKeyPairGenerator)1 ECIESPublicKeyParser (org.spongycastle.crypto.parsers.ECIESPublicKeyParser)1