Java Code Examples for java.security.spec.MGF1ParameterSpec

The following examples show how to use java.security.spec.MGF1ParameterSpec. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
public static void main(String[] argv) throws Exception {
    boolean status = true;
    byte[] p = { (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04 };
    status &= runTest("SHA-224", MGF1ParameterSpec.SHA224, p);
    status &= runTest("SHA-256", MGF1ParameterSpec.SHA256, p);
    status &= runTest("SHA-384", MGF1ParameterSpec.SHA384, p);
    status &= runTest("SHA-512", MGF1ParameterSpec.SHA512, p);
    status &= runTest("SHA", MGF1ParameterSpec.SHA1, new byte[0]);
    status &= runTest("SHA-1", MGF1ParameterSpec.SHA1, new byte[0]);
    status &= runTest("SHA1", MGF1ParameterSpec.SHA1, new byte[0]);
    if (status) {
        System.out.println("Test Passed");
    } else {
        throw new Exception("One or More Test Failed");
    }
}
 
Example 2
public static void main(String[] argv) throws Exception {
    boolean status = true;
    byte[] p = { (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04 };
    status &= runTest("SHA-224", MGF1ParameterSpec.SHA224, p);
    status &= runTest("SHA-256", MGF1ParameterSpec.SHA256, p);
    status &= runTest("SHA-384", MGF1ParameterSpec.SHA384, p);
    status &= runTest("SHA-512", MGF1ParameterSpec.SHA512, p);
    status &= runTest("SHA", MGF1ParameterSpec.SHA1, new byte[0]);
    status &= runTest("SHA-1", MGF1ParameterSpec.SHA1, new byte[0]);
    status &= runTest("SHA1", MGF1ParameterSpec.SHA1, new byte[0]);
    if (status) {
        System.out.println("Test Passed");
    } else {
        throw new Exception("One or More Test Failed");
    }
}
 
Example 3
private static boolean compareMGF(OAEPParameterSpec s1,
    OAEPParameterSpec s2) {
    String alg1 = s1.getMGFAlgorithm();
    String alg2 = s2.getMGFAlgorithm();
    if (alg1.equals(alg2)) {
        MGF1ParameterSpec mp1 = (MGF1ParameterSpec)s1.getMGFParameters();
        MGF1ParameterSpec mp2 = (MGF1ParameterSpec)s2.getMGFParameters();
        alg1 = mp1.getDigestAlgorithm();
        alg2 = mp2.getDigestAlgorithm();
        if (alg1.equals(alg2)) {
            return true;
        } else {
            System.out.println("MGF's MD algos: " + alg1 + " vs " + alg2);
            return false;
        }
    } else {
        System.out.println("MGF algos: " + alg1 + " vs " + alg2);
        return false;
    }
}
 
Example 4
public static void main(String[] argv) throws Exception {
    boolean status = true;
    byte[] p = { (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04 };
    status &= runTest("SHA-224", MGF1ParameterSpec.SHA224, p);
    status &= runTest("SHA-256", MGF1ParameterSpec.SHA256, p);
    status &= runTest("SHA-384", MGF1ParameterSpec.SHA384, p);
    status &= runTest("SHA-512", MGF1ParameterSpec.SHA512, p);
    status &= runTest("SHA", MGF1ParameterSpec.SHA1, new byte[0]);
    status &= runTest("SHA-1", MGF1ParameterSpec.SHA1, new byte[0]);
    status &= runTest("SHA1", MGF1ParameterSpec.SHA1, new byte[0]);
    if (status) {
        System.out.println("Test Passed");
    } else {
        throw new Exception("One or More Test Failed");
    }
}
 
Example 5
private static boolean compareMGF(OAEPParameterSpec s1,
    OAEPParameterSpec s2) {
    String alg1 = s1.getMGFAlgorithm();
    String alg2 = s2.getMGFAlgorithm();
    if (alg1.equals(alg2)) {
        MGF1ParameterSpec mp1 = (MGF1ParameterSpec)s1.getMGFParameters();
        MGF1ParameterSpec mp2 = (MGF1ParameterSpec)s2.getMGFParameters();
        alg1 = mp1.getDigestAlgorithm();
        alg2 = mp2.getDigestAlgorithm();
        if (alg1.equals(alg2)) {
            return true;
        } else {
            System.out.println("MGF's MD algos: " + alg1 + " vs " + alg2);
            return false;
        }
    } else {
        System.out.println("MGF algos: " + alg1 + " vs " + alg2);
        return false;
    }
}
 
Example 6
Source Project: RipplePower   Source File: AlgorithmParametersSpi.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Return the PKCS#1 ASN.1 structure RSAES-OAEP-params.
 */
protected byte[] engineGetEncoded() 
{
    AlgorithmIdentifier hashAlgorithm = new AlgorithmIdentifier(
                                                    DigestFactory.getOID(currentSpec.getDigestAlgorithm()),
                                                    DERNull.INSTANCE);
    MGF1ParameterSpec mgfSpec = (MGF1ParameterSpec)currentSpec.getMGFParameters();
    AlgorithmIdentifier maskGenAlgorithm = new AlgorithmIdentifier(
                                                    PKCSObjectIdentifiers.id_mgf1,
                                                    new AlgorithmIdentifier(DigestFactory.getOID(mgfSpec.getDigestAlgorithm()), DERNull.INSTANCE));
    PSource.PSpecified      pSource = (PSource.PSpecified)currentSpec.getPSource();
    AlgorithmIdentifier pSourceAlgorithm = new AlgorithmIdentifier(
                                                    PKCSObjectIdentifiers.id_pSpecified, new DEROctetString(pSource.getValue()));
    RSAESOAEPparams oaepP = new RSAESOAEPparams(hashAlgorithm, maskGenAlgorithm, pSourceAlgorithm);
    
    try
    {
        return oaepP.getEncoded(ASN1Encoding.DER);
    }
    catch (IOException e)
    {
        throw new RuntimeException("Error encoding OAEPParameters");
    }
}
 
Example 7
Source Project: PFLockScreen-Android   Source File: PFSecurityUtils.java    License: Apache License 2.0 6 votes vote down vote up
private void initEncodeCipher(Cipher cipher, String alias, KeyStore keyStore)
        throws PFSecurityException {
    try {
        final PublicKey key = keyStore.getCertificate(alias).getPublicKey();
        final PublicKey unrestricted = KeyFactory.getInstance(key.getAlgorithm()).generatePublic(
                new X509EncodedKeySpec(key.getEncoded()));
        final OAEPParameterSpec spec = new OAEPParameterSpec("SHA-256", "MGF1",
                MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT);
        cipher.init(Cipher.ENCRYPT_MODE, unrestricted, spec);
    } catch (KeyStoreException | InvalidKeySpecException |
            NoSuchAlgorithmException | InvalidKeyException |
            InvalidAlgorithmParameterException e) {
        throw new PFSecurityException(
                "Can not initialize Encode Cipher:" + e.getMessage(),
                PFSecurityUtilsErrorCodes.ERROR_INIT_ENDECODE_CIPHER
        );
    }
}
 
Example 8
Source Project: openjdk-jdk8u   Source File: PSSParameters.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
protected void engineInit(AlgorithmParameterSpec paramSpec)
        throws InvalidParameterSpecException {
    if (!(paramSpec instanceof PSSParameterSpec)) {
        throw new InvalidParameterSpecException
            ("Inappropriate parameter specification");
    }
    PSSParameterSpec spec = (PSSParameterSpec) paramSpec;

    String mgfName = spec.getMGFAlgorithm();
    if (!spec.getMGFAlgorithm().equalsIgnoreCase("MGF1")) {
        throw new InvalidParameterSpecException("Unsupported mgf " +
            mgfName + "; MGF1 only");
    }
    AlgorithmParameterSpec mgfSpec = spec.getMGFParameters();
    if (!(mgfSpec instanceof MGF1ParameterSpec)) {
        throw new InvalidParameterSpecException("Inappropriate mgf " +
            "parameters; non-null MGF1ParameterSpec only");
    }
    this.spec = spec;
}
 
Example 9
Source Project: openjdk-jdk8u   Source File: CSignature.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
protected boolean engineVerify(byte[] sigBytes) throws SignatureException {
    ensureInit();
    if (fallbackSignature != null) {
        needsReset = false;
        return fallbackSignature.verify(sigBytes);
    } else {
        byte[] hash = getDigestValue();
        return verifyCngSignedHash(
                2, hash, hash.length,
                sigBytes, sigBytes.length,
                pssParams.getSaltLength(),
                ((MGF1ParameterSpec)
                        pssParams.getMGFParameters()).getDigestAlgorithm(),
                publicKey.getHCryptProvider(),
                publicKey.getHCryptKey()
        );
    }
}
 
Example 10
private static boolean compareMGF(OAEPParameterSpec s1,
    OAEPParameterSpec s2) {
    String alg1 = s1.getMGFAlgorithm();
    String alg2 = s2.getMGFAlgorithm();
    if (alg1.equals(alg2)) {
        MGF1ParameterSpec mp1 = (MGF1ParameterSpec)s1.getMGFParameters();
        MGF1ParameterSpec mp2 = (MGF1ParameterSpec)s2.getMGFParameters();
        alg1 = mp1.getDigestAlgorithm();
        alg2 = mp2.getDigestAlgorithm();
        if (alg1.equals(alg2)) {
            return true;
        } else {
            System.out.println("MGF's MD algos: " + alg1 + " vs " + alg2);
            return false;
        }
    } else {
        System.out.println("MGF algos: " + alg1 + " vs " + alg2);
        return false;
    }
}
 
Example 11
private static boolean runTest(String mdName, MGF1ParameterSpec mgfSpec,
    byte[] p) throws Exception {
    OAEPParameterSpec spec = new OAEPParameterSpec(mdName, "MGF1",
        mgfSpec, new PSource.PSpecified(p));
    cp = Security.getProvider("SunJCE");
    System.out.println("Testing provider " + cp.getName() + "...");
    AlgorithmParameters ap = AlgorithmParameters.getInstance("OAEP", cp);

    ap.init(spec);
    byte[] encoding = ap.getEncoded();

    AlgorithmParameters ap2 = AlgorithmParameters.getInstance("OAEP", cp);
    ap2.init(encoding);

    OAEPParameterSpec spec2 = (OAEPParameterSpec) ap2.getParameterSpec
            (OAEPParameterSpec.class);
    return compareSpec(spec, spec2);
}
 
Example 12
private static boolean compareMGF(OAEPParameterSpec s1,
    OAEPParameterSpec s2) {
    String alg1 = s1.getMGFAlgorithm();
    String alg2 = s2.getMGFAlgorithm();
    if (alg1.equals(alg2)) {
        MGF1ParameterSpec mp1 = (MGF1ParameterSpec)s1.getMGFParameters();
        MGF1ParameterSpec mp2 = (MGF1ParameterSpec)s2.getMGFParameters();
        alg1 = mp1.getDigestAlgorithm();
        alg2 = mp2.getDigestAlgorithm();
        if (alg1.equals(alg2)) {
            return true;
        } else {
            System.out.println("MGF's MD algos: " + alg1 + " vs " + alg2);
            return false;
        }
    } else {
        System.out.println("MGF algos: " + alg1 + " vs " + alg2);
        return false;
    }
}
 
Example 13
public static void main(String[] argv) throws Exception {
    boolean status = true;
    byte[] p = { (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04 };
    status &= runTest("SHA-224", MGF1ParameterSpec.SHA224, p);
    status &= runTest("SHA-256", MGF1ParameterSpec.SHA256, p);
    status &= runTest("SHA-384", MGF1ParameterSpec.SHA384, p);
    status &= runTest("SHA-512", MGF1ParameterSpec.SHA512, p);
    status &= runTest("SHA-512/224", MGF1ParameterSpec.SHA512_224, p);
    status &= runTest("SHA-512/256", MGF1ParameterSpec.SHA512_256, p);
    status &= runTest("SHA", MGF1ParameterSpec.SHA1, new byte[0]);
    status &= runTest("SHA-1", MGF1ParameterSpec.SHA1, new byte[0]);
    status &= runTest("SHA1", MGF1ParameterSpec.SHA1, new byte[0]);
    if (status) {
        System.out.println("Test Passed");
    } else {
        throw new Exception("One or More Test Failed");
    }
}
 
Example 14
Source Project: samples-android   Source File: BaseEncryptionManager.java    License: Apache License 2.0 6 votes vote down vote up
private void initEncodeCipher(String keyAlias, int mode) throws GeneralSecurityException {
    PublicKey key = mKeyStore.getCertificate(keyAlias).getPublicKey();

    // workaround for using public key
    // from https://developer.android.com/reference/android/security/keystore/KeyGenParameterSpec.html#known-issues
    PublicKey unrestricted = KeyFactory.getInstance(key.getAlgorithm())
            .generatePublic(new X509EncodedKeySpec(key.getEncoded()));

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        // from https://code.google.com/p/android/issues/detail?id=197719
        OAEPParameterSpec spec = new OAEPParameterSpec("SHA-256", "MGF1",
                MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT);

        mCipher.init(mode, unrestricted, spec);
    } else {
        mCipher.init(mode, unrestricted);
    }
}
 
Example 15
private static boolean runTest(String mdName, MGF1ParameterSpec mgfSpec,
    byte[] p) throws Exception {
    OAEPParameterSpec spec = new OAEPParameterSpec(mdName, "MGF1",
        mgfSpec, new PSource.PSpecified(p));
    cp = Security.getProvider("SunJCE");
    System.out.println("Testing provider " + cp.getName() + "...");
    AlgorithmParameters ap = AlgorithmParameters.getInstance("OAEP", cp);

    ap.init(spec);
    byte[] encoding = ap.getEncoded();

    AlgorithmParameters ap2 = AlgorithmParameters.getInstance("OAEP", cp);
    ap2.init(encoding);

    OAEPParameterSpec spec2 = (OAEPParameterSpec) ap2.getParameterSpec
            (OAEPParameterSpec.class);
    return compareSpec(spec, spec2);
}
 
Example 16
private static boolean runTest(String mdName, MGF1ParameterSpec mgfSpec,
    byte[] p) throws Exception {
    OAEPParameterSpec spec = new OAEPParameterSpec(mdName, "MGF1",
        mgfSpec, new PSource.PSpecified(p));
    cp = Security.getProvider("SunJCE");
    System.out.println("Testing provider " + cp.getName() + "...");
    AlgorithmParameters ap = AlgorithmParameters.getInstance("OAEP", cp);

    ap.init(spec);
    byte[] encoding = ap.getEncoded();

    AlgorithmParameters ap2 = AlgorithmParameters.getInstance("OAEP", cp);
    ap2.init(encoding);

    OAEPParameterSpec spec2 = (OAEPParameterSpec) ap2.getParameterSpec
            (OAEPParameterSpec.class);
    return compareSpec(spec, spec2);
}
 
Example 17
private static boolean compareMGF(OAEPParameterSpec s1,
    OAEPParameterSpec s2) {
    String alg1 = s1.getMGFAlgorithm();
    String alg2 = s2.getMGFAlgorithm();
    if (alg1.equals(alg2)) {
        MGF1ParameterSpec mp1 = (MGF1ParameterSpec)s1.getMGFParameters();
        MGF1ParameterSpec mp2 = (MGF1ParameterSpec)s2.getMGFParameters();
        alg1 = mp1.getDigestAlgorithm();
        alg2 = mp2.getDigestAlgorithm();
        if (alg1.equals(alg2)) {
            return true;
        } else {
            System.out.println("MGF's MD algos: " + alg1 + " vs " + alg2);
            return false;
        }
    } else {
        System.out.println("MGF algos: " + alg1 + " vs " + alg2);
        return false;
    }
}
 
Example 18
public static void main(String[] argv) throws Exception {
    boolean status = true;
    byte[] p = { (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04 };
    status &= runTest("SHA-224", MGF1ParameterSpec.SHA224, p);
    status &= runTest("SHA-256", MGF1ParameterSpec.SHA256, p);
    status &= runTest("SHA-384", MGF1ParameterSpec.SHA384, p);
    status &= runTest("SHA-512", MGF1ParameterSpec.SHA512, p);
    status &= runTest("SHA", MGF1ParameterSpec.SHA1, new byte[0]);
    status &= runTest("SHA-1", MGF1ParameterSpec.SHA1, new byte[0]);
    status &= runTest("SHA1", MGF1ParameterSpec.SHA1, new byte[0]);
    if (status) {
        System.out.println("Test Passed");
    } else {
        throw new Exception("One or More Test Failed");
    }
}
 
Example 19
Source Project: RipplePower   Source File: AlgorithmParametersSpi.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Return the PKCS#1 ASN.1 structure RSASSA-PSS-params.
 */
protected byte[] engineGetEncoded() 
    throws IOException
{
    PSSParameterSpec pssSpec = currentSpec;
    AlgorithmIdentifier hashAlgorithm = new AlgorithmIdentifier(
                                        DigestFactory.getOID(pssSpec.getDigestAlgorithm()),
                                        DERNull.INSTANCE);
    MGF1ParameterSpec mgfSpec = (MGF1ParameterSpec)pssSpec.getMGFParameters();
    AlgorithmIdentifier maskGenAlgorithm = new AlgorithmIdentifier(
                                        PKCSObjectIdentifiers.id_mgf1,
                                        new AlgorithmIdentifier(DigestFactory.getOID(mgfSpec.getDigestAlgorithm()), DERNull.INSTANCE));
    RSASSAPSSparams pssP = new RSASSAPSSparams(hashAlgorithm, maskGenAlgorithm, new ASN1Integer(pssSpec.getSaltLength()), new ASN1Integer(pssSpec.getTrailerField()));
    
    return pssP.getEncoded("DER");
}
 
Example 20
private static boolean runTest(String mdName, MGF1ParameterSpec mgfSpec,
    byte[] p) throws Exception {
    OAEPParameterSpec spec = new OAEPParameterSpec(mdName, "MGF1",
        mgfSpec, new PSource.PSpecified(p));
    cp = Security.getProvider("SunJCE");
    System.out.println("Testing provider " + cp.getName() + "...");
    AlgorithmParameters ap = AlgorithmParameters.getInstance("OAEP", cp);

    ap.init(spec);
    byte[] encoding = ap.getEncoded();

    AlgorithmParameters ap2 = AlgorithmParameters.getInstance("OAEP", cp);
    ap2.init(encoding);

    OAEPParameterSpec spec2 = (OAEPParameterSpec) ap2.getParameterSpec
            (OAEPParameterSpec.class);
    return compareSpec(spec, spec2);
}
 
Example 21
private static boolean compareMGF(OAEPParameterSpec s1,
    OAEPParameterSpec s2) {
    String alg1 = s1.getMGFAlgorithm();
    String alg2 = s2.getMGFAlgorithm();
    if (alg1.equals(alg2)) {
        MGF1ParameterSpec mp1 = (MGF1ParameterSpec)s1.getMGFParameters();
        MGF1ParameterSpec mp2 = (MGF1ParameterSpec)s2.getMGFParameters();
        alg1 = mp1.getDigestAlgorithm();
        alg2 = mp2.getDigestAlgorithm();
        if (alg1.equals(alg2)) {
            return true;
        } else {
            System.out.println("MGF's MD algos: " + alg1 + " vs " + alg2);
            return false;
        }
    } else {
        System.out.println("MGF algos: " + alg1 + " vs " + alg2);
        return false;
    }
}
 
Example 22
public static void main(String[] argv) throws Exception {
    boolean status = true;
    byte[] p = { (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04 };
    status &= runTest("SHA-224", MGF1ParameterSpec.SHA224, p);
    status &= runTest("SHA-256", MGF1ParameterSpec.SHA256, p);
    status &= runTest("SHA-384", MGF1ParameterSpec.SHA384, p);
    status &= runTest("SHA-512", MGF1ParameterSpec.SHA512, p);
    status &= runTest("SHA", MGF1ParameterSpec.SHA1, new byte[0]);
    status &= runTest("SHA-1", MGF1ParameterSpec.SHA1, new byte[0]);
    status &= runTest("SHA1", MGF1ParameterSpec.SHA1, new byte[0]);
    if (status) {
        System.out.println("Test Passed");
    } else {
        throw new Exception("One or More Test Failed");
    }
}
 
Example 23
private static boolean runTest(String mdName, MGF1ParameterSpec mgfSpec,
    byte[] p) throws Exception {
    OAEPParameterSpec spec = new OAEPParameterSpec(mdName, "MGF1",
        mgfSpec, new PSource.PSpecified(p));
    cp = Security.getProvider("SunJCE");
    System.out.println("Testing provider " + cp.getName() + "...");
    AlgorithmParameters ap = AlgorithmParameters.getInstance("OAEP", cp);

    ap.init(spec);
    byte[] encoding = ap.getEncoded();

    AlgorithmParameters ap2 = AlgorithmParameters.getInstance("OAEP", cp);
    ap2.init(encoding);

    OAEPParameterSpec spec2 = (OAEPParameterSpec) ap2.getParameterSpec
            (OAEPParameterSpec.class);
    return compareSpec(spec, spec2);
}
 
Example 24
private static boolean compareMGF(OAEPParameterSpec s1,
    OAEPParameterSpec s2) {
    String alg1 = s1.getMGFAlgorithm();
    String alg2 = s2.getMGFAlgorithm();
    if (alg1.equals(alg2)) {
        MGF1ParameterSpec mp1 = (MGF1ParameterSpec)s1.getMGFParameters();
        MGF1ParameterSpec mp2 = (MGF1ParameterSpec)s2.getMGFParameters();
        alg1 = mp1.getDigestAlgorithm();
        alg2 = mp2.getDigestAlgorithm();
        if (alg1.equals(alg2)) {
            return true;
        } else {
            System.out.println("MGF's MD algos: " + alg1 + " vs " + alg2);
            return false;
        }
    } else {
        System.out.println("MGF algos: " + alg1 + " vs " + alg2);
        return false;
    }
}
 
Example 25
public static void main(String[] argv) throws Exception {
    boolean status = true;
    byte[] p = { (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04 };
    status &= runTest("SHA-224", MGF1ParameterSpec.SHA224, p);
    status &= runTest("SHA-256", MGF1ParameterSpec.SHA256, p);
    status &= runTest("SHA-384", MGF1ParameterSpec.SHA384, p);
    status &= runTest("SHA-512", MGF1ParameterSpec.SHA512, p);
    status &= runTest("SHA", MGF1ParameterSpec.SHA1, new byte[0]);
    status &= runTest("SHA-1", MGF1ParameterSpec.SHA1, new byte[0]);
    status &= runTest("SHA1", MGF1ParameterSpec.SHA1, new byte[0]);
    if (status) {
        System.out.println("Test Passed");
    } else {
        throw new Exception("One or More Test Failed");
    }
}
 
Example 26
Source Project: hottub   Source File: TestOAEPParameterSpec.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static boolean runTest(String mdName, MGF1ParameterSpec mgfSpec,
    byte[] p) throws Exception {
    OAEPParameterSpec spec = new OAEPParameterSpec(mdName, "MGF1",
        mgfSpec, new PSource.PSpecified(p));
    cp = Security.getProvider("SunJCE");
    System.out.println("Testing provider " + cp.getName() + "...");
    AlgorithmParameters ap = AlgorithmParameters.getInstance("OAEP", cp);

    ap.init(spec);
    byte[] encoding = ap.getEncoded();

    AlgorithmParameters ap2 = AlgorithmParameters.getInstance("OAEP", cp);
    ap2.init(encoding);

    OAEPParameterSpec spec2 = (OAEPParameterSpec) ap2.getParameterSpec
            (OAEPParameterSpec.class);
    return compareSpec(spec, spec2);
}
 
Example 27
Source Project: hottub   Source File: TestOAEPParameterSpec.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static boolean compareMGF(OAEPParameterSpec s1,
    OAEPParameterSpec s2) {
    String alg1 = s1.getMGFAlgorithm();
    String alg2 = s2.getMGFAlgorithm();
    if (alg1.equals(alg2)) {
        MGF1ParameterSpec mp1 = (MGF1ParameterSpec)s1.getMGFParameters();
        MGF1ParameterSpec mp2 = (MGF1ParameterSpec)s2.getMGFParameters();
        alg1 = mp1.getDigestAlgorithm();
        alg2 = mp2.getDigestAlgorithm();
        if (alg1.equals(alg2)) {
            return true;
        } else {
            System.out.println("MGF's MD algos: " + alg1 + " vs " + alg2);
            return false;
        }
    } else {
        System.out.println("MGF algos: " + alg1 + " vs " + alg2);
        return false;
    }
}
 
Example 28
private static boolean runTest(String mdName, MGF1ParameterSpec mgfSpec,
    byte[] p) throws Exception {
    OAEPParameterSpec spec = new OAEPParameterSpec(mdName, "MGF1",
        mgfSpec, new PSource.PSpecified(p));
    cp = Security.getProvider("SunJCE");
    System.out.println("Testing provider " + cp.getName() + "...");
    AlgorithmParameters ap = AlgorithmParameters.getInstance("OAEP", cp);

    ap.init(spec);
    byte[] encoding = ap.getEncoded();

    AlgorithmParameters ap2 = AlgorithmParameters.getInstance("OAEP", cp);
    ap2.init(encoding);

    OAEPParameterSpec spec2 = (OAEPParameterSpec) ap2.getParameterSpec
            (OAEPParameterSpec.class);
    return compareSpec(spec, spec2);
}
 
Example 29
public static void main(String[] argv) throws Exception {
    boolean status = true;
    byte[] p = { (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04 };
    status &= runTest("SHA-224", MGF1ParameterSpec.SHA224, p);
    status &= runTest("SHA-256", MGF1ParameterSpec.SHA256, p);
    status &= runTest("SHA-384", MGF1ParameterSpec.SHA384, p);
    status &= runTest("SHA-512", MGF1ParameterSpec.SHA512, p);
    status &= runTest("SHA", MGF1ParameterSpec.SHA1, new byte[0]);
    status &= runTest("SHA-1", MGF1ParameterSpec.SHA1, new byte[0]);
    status &= runTest("SHA1", MGF1ParameterSpec.SHA1, new byte[0]);
    if (status) {
        System.out.println("Test Passed");
    } else {
        throw new Exception("One or More Test Failed");
    }
}
 
Example 30
private static boolean runTest(String mdName, MGF1ParameterSpec mgfSpec,
    byte[] p) throws Exception {
    OAEPParameterSpec spec = new OAEPParameterSpec(mdName, "MGF1",
        mgfSpec, new PSource.PSpecified(p));
    cp = Security.getProvider("SunJCE");
    System.out.println("Testing provider " + cp.getName() + "...");
    AlgorithmParameters ap = AlgorithmParameters.getInstance("OAEP", cp);

    ap.init(spec);
    byte[] encoding = ap.getEncoded();

    AlgorithmParameters ap2 = AlgorithmParameters.getInstance("OAEP", cp);
    ap2.init(encoding);

    OAEPParameterSpec spec2 = (OAEPParameterSpec) ap2.getParameterSpec
            (OAEPParameterSpec.class);
    return compareSpec(spec, spec2);
}