jdk.testlibrary.RandomFactory Java Examples

The following examples show how to use jdk.testlibrary.RandomFactory. 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
Source File: EntryProtectionTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private void setUp() {
    out.println("Using KEYSTORE_PATH:"+KEYSTORE_PATH);
    Utils.createKeyStore(Utils.KeyStoreType.pkcs12, KEYSTORE_PATH, ALIAS);
    Random rand = RandomFactory.getRandom();
    rand.nextBytes(SALT);
    out.print("Salt: ");
    for (byte b : SALT) {
        out.format("%02X ", b);
    }
    out.println("");
    PASSWORD_PROTECTION
            .add(new KeyStore.PasswordProtection(PASSWORD,
                            "PBEWithMD5AndDES", new PBEParameterSpec(SALT,
                                    ITERATION_COUNT)));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndDESede", null));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndRC2_40", null));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndRC2_128", null));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndRC4_40", null));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndRC4_128", null));
}
 
Example #2
Source File: CipherNCFuncTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws ShortBufferException,
        IllegalBlockSizeException, BadPaddingException {
    byte[] plainText = new byte[801];
    // Initialization
    RandomFactory.getRandom().nextBytes(plainText);
    Cipher ci = new NullCipher();
    // Encryption
    byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];
    int offset = ci.update(plainText, 0, plainText.length, cipherText, 0);
    ci.doFinal(cipherText, offset);
    // Decryption
    byte[] recoveredText = new byte[ci.getOutputSize(cipherText.length)];
    int len = ci.doFinal(cipherText, 0, cipherText.length, recoveredText);
    // Comparison
    if (len != plainText.length ||
            !TestUtilities.equalsBlock(plainText, cipherText, len) ||
            !TestUtilities.equalsBlock(plainText, recoveredText, len)) {
        throw new RuntimeException(
            "Test failed because plainText not equal to cipherText and revoveredText");
    }
}
 
Example #3
Source File: TestAvailable.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String args[]) throws Throwable {
    Random r = RandomFactory.getRandom();
    for (int n = 0; n < 10; n++) {
        byte[] src = new byte[r.nextInt(100) + 1];
        r.nextBytes(src);
        // test InflaterInputStream
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try (DeflaterOutputStream dos = new DeflaterOutputStream(baos)) {
            dos.write(src);
        }
        try (InflaterInputStream iis = new InflaterInputStream(
               new ByteArrayInputStream(baos.toByteArray()))) {
            test(iis, src);
        }

        // test GZIPInputStream
        baos = new ByteArrayOutputStream();
        try (GZIPOutputStream dos = new GZIPOutputStream(baos)) {
            dos.write(src);
        }
        try (GZIPInputStream gis = new GZIPInputStream(
               new ByteArrayInputStream(baos.toByteArray()))) {
            test(gis, src);
        }
    }
}
 
Example #4
Source File: DTLSIncorrectAppDataTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private void checkIncorrectAppDataUnwrap(SSLEngine sendEngine,
        SSLEngine recvEngine) throws SSLException {
    String direction = sendEngine.getUseClientMode() ? "client"
            : "server";
    System.out.println("================================================="
            + "===========");
    System.out.println("Testing DTLS incorrect app data packages unwrapping"
            + " by sending data from " + direction);
    ByteBuffer app = ByteBuffer.wrap(MESSAGE.getBytes());
    ByteBuffer net = doWrap(sendEngine, direction, 0, app);
    final Random RNG = RandomFactory.getRandom();
    int randomPlace = RNG.nextInt(net.remaining());
    net.array()[randomPlace] += 1;
    app = ByteBuffer.allocate(recvEngine.getSession()
            .getApplicationBufferSize());
    recvEngine.unwrap(net, app);
    app.flip();
    int length = app.remaining();
    System.out.println("Unwrapped " + length + " bytes.");
}
 
Example #5
Source File: EntryProtectionTest.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private void setUp() {
    out.println("Using KEYSTORE_PATH:"+KEYSTORE_PATH);
    Utils.createKeyStore(Utils.KeyStoreType.pkcs12, KEYSTORE_PATH, ALIAS);
    Random rand = RandomFactory.getRandom();
    rand.nextBytes(SALT);
    out.print("Salt: ");
    for (byte b : SALT) {
        out.format("%02X ", b);
    }
    out.println("");
    PASSWORD_PROTECTION
            .add(new KeyStore.PasswordProtection(PASSWORD,
                            "PBEWithMD5AndDES", new PBEParameterSpec(SALT,
                                    ITERATION_COUNT)));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndDESede", null));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndRC2_40", null));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndRC2_128", null));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndRC4_40", null));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndRC4_128", null));
}
 
Example #6
Source File: EntryProtectionTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private void setUp() {
    out.println("Using KEYSTORE_PATH:"+KEYSTORE_PATH);
    Utils.createKeyStore(Utils.KeyStoreType.pkcs12, KEYSTORE_PATH, ALIAS);
    Random rand = RandomFactory.getRandom();
    rand.nextBytes(SALT);
    out.print("Salt: ");
    for (byte b : SALT) {
        out.format("%02X ", b);
    }
    out.println("");
    PASSWORD_PROTECTION
            .add(new KeyStore.PasswordProtection(PASSWORD,
                            "PBEWithMD5AndDES", new PBEParameterSpec(SALT,
                                    ITERATION_COUNT)));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndDESede", null));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndRC2_40", null));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndRC2_128", null));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndRC4_40", null));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndRC4_128", null));
}
 
Example #7
Source File: SignatureTest.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    String testAlg = args[0];
    int testSize = Integer.parseInt(args[1]);

    byte[] data = new byte[100];
    RandomFactory.getRandom().nextBytes(data);

    // create a key pair
    KeyPair kpair = generateKeys(KEYALG, testSize);
    Key[] privs = manipulateKey(PRIVATE_KEY, kpair.getPrivate());
    Key[] pubs = manipulateKey(PUBLIC_KEY, kpair.getPublic());
    // For signature algorithm, create and verify a signature

    Arrays.stream(privs).forEach(priv
            -> Arrays.stream(pubs).forEach(pub -> {
                try {
                    checkSignature(data, (PublicKey) pub, (PrivateKey) priv,
                            testAlg);
                } catch (NoSuchAlgorithmException | InvalidKeyException
                        | SignatureException | NoSuchProviderException ex) {
                    throw new RuntimeException(ex);
                }
            }
            ));

}
 
Example #8
Source File: CipherNCFuncTest.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws ShortBufferException,
        IllegalBlockSizeException, BadPaddingException {
    byte[] plainText = new byte[801];
    // Initialization
    RandomFactory.getRandom().nextBytes(plainText);
    Cipher ci = new NullCipher();
    // Encryption
    byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];
    int offset = ci.update(plainText, 0, plainText.length, cipherText, 0);
    ci.doFinal(cipherText, offset);
    // Decryption
    byte[] recoveredText = new byte[ci.getOutputSize(cipherText.length)];
    int len = ci.doFinal(cipherText, 0, cipherText.length, recoveredText);
    // Comparison
    if (len != plainText.length ||
            !TestUtilities.equalsBlock(plainText, cipherText, len) ||
            !TestUtilities.equalsBlock(plainText, recoveredText, len)) {
        throw new RuntimeException(
            "Test failed because plainText not equal to cipherText and revoveredText");
    }
}
 
Example #9
Source File: ModPow65537.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static void testSigning(KeyPair kp) throws Exception {
    System.out.println(kp.getPublic());
    byte[] data = new byte[1024];
    Random random = RandomFactory.getRandom();
    random.nextBytes(data);

    Signature sig = Signature.getInstance("SHA1withRSA", "SunRsaSign");
    sig.initSign(kp.getPrivate());
    sig.update(data);
    byte[] sigBytes = sig.sign();

    sig.initVerify(kp.getPublic());
    sig.update(data);
    if (sig.verify(sigBytes) == false) {
        throw new Exception("signature verification failed");
    }
    System.out.println("OK");
}
 
Example #10
Source File: EntryProtectionTest.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private void setUp() {
    out.println("Using KEYSTORE_PATH:"+KEYSTORE_PATH);
    Utils.createKeyStore(Utils.KeyStoreType.pkcs12, KEYSTORE_PATH, ALIAS);
    Random rand = RandomFactory.getRandom();
    rand.nextBytes(SALT);
    out.print("Salt: ");
    for (byte b : SALT) {
        out.format("%02X ", b);
    }
    out.println("");
    PASSWORD_PROTECTION
            .add(new KeyStore.PasswordProtection(PASSWORD,
                            "PBEWithMD5AndDES", new PBEParameterSpec(SALT,
                                    ITERATION_COUNT)));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndDESede", null));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndRC2_40", null));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndRC2_128", null));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndRC4_40", null));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndRC4_128", null));
}
 
Example #11
Source File: CipherNCFuncTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws ShortBufferException,
        IllegalBlockSizeException, BadPaddingException {
    byte[] plainText = new byte[801];
    // Initialization
    RandomFactory.getRandom().nextBytes(plainText);
    Cipher ci = new NullCipher();
    // Encryption
    byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];
    int offset = ci.update(plainText, 0, plainText.length, cipherText, 0);
    ci.doFinal(cipherText, offset);
    // Decryption
    byte[] recoveredText = new byte[ci.getOutputSize(cipherText.length)];
    int len = ci.doFinal(cipherText, 0, cipherText.length, recoveredText);
    // Comparison
    if (len != plainText.length ||
            !TestUtilities.equalsBlock(plainText, cipherText, len) ||
            !TestUtilities.equalsBlock(plainText, recoveredText, len)) {
        throw new RuntimeException(
            "Test failed because plainText not equal to cipherText and revoveredText");
    }
}
 
Example #12
Source File: SignatureTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    String testAlg = args[0];
    int testSize = Integer.parseInt(args[1]);

    byte[] data = new byte[100];
    RandomFactory.getRandom().nextBytes(data);

    // create a key pair
    KeyPair kpair = generateKeys(KEYALG, testSize);
    Key[] privs = manipulateKey(PRIVATE_KEY, kpair.getPrivate());
    Key[] pubs = manipulateKey(PUBLIC_KEY, kpair.getPublic());
    // For signature algorithm, create and verify a signature

    Arrays.stream(privs).forEach(priv
            -> Arrays.stream(pubs).forEach(pub -> {
                try {
                    checkSignature(data, (PublicKey) pub, (PrivateKey) priv,
                            testAlg);
                } catch (NoSuchAlgorithmException | InvalidKeyException
                        | SignatureException | NoSuchProviderException ex) {
                    throw new RuntimeException(ex);
                }
            }
            ));

}
 
Example #13
Source File: CipherNCFuncTest.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws ShortBufferException,
        IllegalBlockSizeException, BadPaddingException {
    byte[] plainText = new byte[801];
    // Initialization
    RandomFactory.getRandom().nextBytes(plainText);
    Cipher ci = new NullCipher();
    // Encryption
    byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];
    int offset = ci.update(plainText, 0, plainText.length, cipherText, 0);
    ci.doFinal(cipherText, offset);
    // Decryption
    byte[] recoveredText = new byte[ci.getOutputSize(cipherText.length)];
    int len = ci.doFinal(cipherText, 0, cipherText.length, recoveredText);
    // Comparison
    if (len != plainText.length ||
            !TestUtilities.equalsBlock(plainText, cipherText, len) ||
            !TestUtilities.equalsBlock(plainText, recoveredText, len)) {
        throw new RuntimeException(
            "Test failed because plainText not equal to cipherText and revoveredText");
    }
}
 
Example #14
Source File: CipherNCFuncTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws ShortBufferException,
        IllegalBlockSizeException, BadPaddingException {
    byte[] plainText = new byte[801];
    // Initialization
    RandomFactory.getRandom().nextBytes(plainText);
    Cipher ci = new NullCipher();
    // Encryption
    byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];
    int offset = ci.update(plainText, 0, plainText.length, cipherText, 0);
    ci.doFinal(cipherText, offset);
    // Decryption
    byte[] recoveredText = new byte[ci.getOutputSize(cipherText.length)];
    int len = ci.doFinal(cipherText, 0, cipherText.length, recoveredText);
    // Comparison
    if (len != plainText.length ||
            !TestUtilities.equalsBlock(plainText, cipherText, len) ||
            !TestUtilities.equalsBlock(plainText, recoveredText, len)) {
        throw new RuntimeException(
            "Test failed because plainText not equal to cipherText and revoveredText");
    }
}
 
Example #15
Source File: SignatureTest.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    String testAlg = args[0];
    int keySize = Integer.parseInt(args[1]);
    Iterable<String> md_alg_pkcs15 =
        SigTestUtil.getDigestAlgorithms(SignatureType.RSA, keySize);

    Iterable<String> md_alg_pss =
        SigTestUtil.getDigestAlgorithms(SignatureType.RSASSA_PSS, keySize);

    byte[] data = new byte[100];
    RandomFactory.getRandom().nextBytes(data);

    // create a key pair
    KeyPair kpair = generateKeys(KEYALG, keySize);
    Key[] privs = manipulateKey(PRIVATE_KEY, kpair.getPrivate());
    Key[] pubs = manipulateKey(PUBLIC_KEY, kpair.getPublic());

    test(SignatureType.RSA, md_alg_pkcs15, privs, pubs, data);
    test(SignatureType.RSASSA_PSS, md_alg_pss, privs, pubs, data);
}
 
Example #16
Source File: EntryProtectionTest.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private void setUp() {
    out.println("Using KEYSTORE_PATH:"+KEYSTORE_PATH);
    Utils.createKeyStore(Utils.KeyStoreType.pkcs12, KEYSTORE_PATH, ALIAS);
    Random rand = RandomFactory.getRandom();
    rand.nextBytes(SALT);
    out.print("Salt: ");
    for (byte b : SALT) {
        out.format("%02X ", b);
    }
    out.println("");
    PASSWORD_PROTECTION
            .add(new KeyStore.PasswordProtection(PASSWORD,
                            "PBEWithMD5AndDES", new PBEParameterSpec(SALT,
                                    ITERATION_COUNT)));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndDESede", null));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndRC2_40", null));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndRC2_128", null));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndRC4_40", null));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndRC4_128", null));
}
 
Example #17
Source File: SignatureTest.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    String testAlg = args[0];
    int testSize = Integer.parseInt(args[1]);

    byte[] data = new byte[100];
    RandomFactory.getRandom().nextBytes(data);

    // create a key pair
    KeyPair kpair = generateKeys(KEYALG, testSize);
    Key[] privs = manipulateKey(PRIVATE_KEY, kpair.getPrivate());
    Key[] pubs = manipulateKey(PUBLIC_KEY, kpair.getPublic());
    // For signature algorithm, create and verify a signature

    Arrays.stream(privs).forEach(priv
            -> Arrays.stream(pubs).forEach(pub -> {
                try {
                    checkSignature(data, (PublicKey) pub, (PrivateKey) priv,
                            testAlg);
                } catch (NoSuchAlgorithmException | InvalidKeyException
                        | SignatureException | NoSuchProviderException ex) {
                    throw new RuntimeException(ex);
                }
            }
            ));

}
 
Example #18
Source File: CipherNCFuncTest.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws ShortBufferException,
        IllegalBlockSizeException, BadPaddingException {
    byte[] plainText = new byte[801];
    // Initialization
    RandomFactory.getRandom().nextBytes(plainText);
    Cipher ci = new NullCipher();
    // Encryption
    byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];
    int offset = ci.update(plainText, 0, plainText.length, cipherText, 0);
    ci.doFinal(cipherText, offset);
    // Decryption
    byte[] recoveredText = new byte[ci.getOutputSize(cipherText.length)];
    int len = ci.doFinal(cipherText, 0, cipherText.length, recoveredText);
    // Comparison
    if (len != plainText.length ||
            !TestUtilities.equalsBlock(plainText, cipherText, len) ||
            !TestUtilities.equalsBlock(plainText, recoveredText, len)) {
        throw new RuntimeException(
            "Test failed because plainText not equal to cipherText and revoveredText");
    }
}
 
Example #19
Source File: EntryProtectionTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private void setUp() {
    out.println("Using KEYSTORE_PATH:"+KEYSTORE_PATH);
    Utils.createKeyStore(Utils.KeyStoreType.pkcs12, KEYSTORE_PATH, ALIAS);
    Random rand = RandomFactory.getRandom();
    rand.nextBytes(SALT);
    out.print("Salt: ");
    for (byte b : SALT) {
        out.format("%02X ", b);
    }
    out.println("");
    PASSWORD_PROTECTION
            .add(new KeyStore.PasswordProtection(PASSWORD,
                            "PBEWithMD5AndDES", new PBEParameterSpec(SALT,
                                    ITERATION_COUNT)));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndDESede", null));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndRC2_40", null));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndRC2_128", null));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndRC4_40", null));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndRC4_128", null));
}
 
Example #20
Source File: CipherNCFuncTest.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws ShortBufferException,
        IllegalBlockSizeException, BadPaddingException {
    byte[] plainText = new byte[801];
    // Initialization
    RandomFactory.getRandom().nextBytes(plainText);
    Cipher ci = new NullCipher();
    // Encryption
    byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];
    int offset = ci.update(plainText, 0, plainText.length, cipherText, 0);
    ci.doFinal(cipherText, offset);
    // Decryption
    byte[] recoveredText = new byte[ci.getOutputSize(cipherText.length)];
    int len = ci.doFinal(cipherText, 0, cipherText.length, recoveredText);
    // Comparison
    if (len != plainText.length ||
            !TestUtilities.equalsBlock(plainText, cipherText, len) ||
            !TestUtilities.equalsBlock(plainText, recoveredText, len)) {
        throw new RuntimeException(
            "Test failed because plainText not equal to cipherText and revoveredText");
    }
}
 
Example #21
Source File: SignatureTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    String testAlg = args[0];
    int keySize = Integer.parseInt(args[1]);
    Iterable<String> md_alg_pkcs15 =
        SigTestUtil.getDigestAlgorithms(SignatureType.RSA, keySize);

    Iterable<String> md_alg_pss =
        SigTestUtil.getDigestAlgorithms(SignatureType.RSASSA_PSS, keySize);

    byte[] data = new byte[100];
    RandomFactory.getRandom().nextBytes(data);

    // create a key pair
    KeyPair kpair = generateKeys(KEYALG, keySize);
    Key[] privs = manipulateKey(PRIVATE_KEY, kpair.getPrivate());
    Key[] pubs = manipulateKey(PUBLIC_KEY, kpair.getPublic());

    test(SignatureType.RSA, md_alg_pkcs15, privs, pubs, data);
    test(SignatureType.RSASSA_PSS, md_alg_pss, privs, pubs, data);
}
 
Example #22
Source File: SignatureTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    String testAlg = args[0];
    int testSize = Integer.parseInt(args[1]);

    byte[] data = new byte[100];
    RandomFactory.getRandom().nextBytes(data);

    // create a key pair
    KeyPair kpair = generateKeys(KEYALG, testSize);
    Key[] privs = manipulateKey(PRIVATE_KEY, kpair.getPrivate());
    Key[] pubs = manipulateKey(PUBLIC_KEY, kpair.getPublic());
    // For signature algorithm, create and verify a signature

    Arrays.stream(privs).forEach(priv
            -> Arrays.stream(pubs).forEach(pub -> {
                try {
                    checkSignature(data, (PublicKey) pub, (PrivateKey) priv,
                            testAlg);
                } catch (NoSuchAlgorithmException | InvalidKeyException
                        | SignatureException | NoSuchProviderException ex) {
                    throw new RuntimeException(ex);
                }
            }
            ));

}
 
Example #23
Source File: CipherNCFuncTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws ShortBufferException,
        IllegalBlockSizeException, BadPaddingException {
    byte[] plainText = new byte[801];
    // Initialization
    RandomFactory.getRandom().nextBytes(plainText);
    Cipher ci = new NullCipher();
    // Encryption
    byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];
    int offset = ci.update(plainText, 0, plainText.length, cipherText, 0);
    ci.doFinal(cipherText, offset);
    // Decryption
    byte[] recoveredText = new byte[ci.getOutputSize(cipherText.length)];
    int len = ci.doFinal(cipherText, 0, cipherText.length, recoveredText);
    // Comparison
    if (len != plainText.length ||
            !TestUtilities.equalsBlock(plainText, cipherText, len) ||
            !TestUtilities.equalsBlock(plainText, recoveredText, len)) {
        throw new RuntimeException(
            "Test failed because plainText not equal to cipherText and revoveredText");
    }
}
 
Example #24
Source File: EntryProtectionTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private void setUp() {
    out.println("Using KEYSTORE_PATH:"+KEYSTORE_PATH);
    Utils.createKeyStore(Utils.KeyStoreType.pkcs12, KEYSTORE_PATH, ALIAS);
    Random rand = RandomFactory.getRandom();
    rand.nextBytes(SALT);
    out.print("Salt: ");
    for (byte b : SALT) {
        out.format("%02X ", b);
    }
    out.println("");
    PASSWORD_PROTECTION
            .add(new KeyStore.PasswordProtection(PASSWORD,
                            "PBEWithMD5AndDES", new PBEParameterSpec(SALT,
                                    ITERATION_COUNT)));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndDESede", null));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndRC2_40", null));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndRC2_128", null));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndRC4_40", null));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndRC4_128", null));
}
 
Example #25
Source File: EntryProtectionTest.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
private void setUp() {
    out.println("Using KEYSTORE_PATH:"+KEYSTORE_PATH);
    Utils.createKeyStore(Utils.KeyStoreType.pkcs12, KEYSTORE_PATH, ALIAS);
    Random rand = RandomFactory.getRandom();
    rand.nextBytes(SALT);
    out.print("Salt: ");
    for (byte b : SALT) {
        out.format("%02X ", b);
    }
    out.println("");
    PASSWORD_PROTECTION
            .add(new KeyStore.PasswordProtection(PASSWORD,
                            "PBEWithMD5AndDES", new PBEParameterSpec(SALT,
                                    ITERATION_COUNT)));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndDESede", null));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndRC2_40", null));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndRC2_128", null));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndRC4_40", null));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndRC4_128", null));
}
 
Example #26
Source File: TestZoneTextPrinterParser.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public void test_printText() {
    Random r = RandomFactory.getRandom();
    int N = 8;
    Locale[] locales = Locale.getAvailableLocales();
    Set<String> zids = ZoneRulesProvider.getAvailableZoneIds();
    ZonedDateTime zdt = ZonedDateTime.now();

    //System.out.printf("locale==%d, timezone=%d%n", locales.length, zids.size());
    while (N-- > 0) {
        zdt = zdt.withDayOfYear(r.nextInt(365) + 1)
                 .with(ChronoField.SECOND_OF_DAY, r.nextInt(86400));
        for (String zid : zids) {
            if (zid.equals("ROC") || zid.startsWith("Etc/GMT")) {
                continue;      // TBD: match jdk behavior?
            }
            zdt = zdt.withZoneSameLocal(ZoneId.of(zid));
            TimeZone tz = TimeZone.getTimeZone(zid);
            boolean isDST = tz.inDaylightTime(new Date(zdt.toInstant().toEpochMilli()));
            for (Locale locale : locales) {
                String longDisplayName = tz.getDisplayName(isDST, TimeZone.LONG, locale);
                String shortDisplayName = tz.getDisplayName(isDST, TimeZone.SHORT, locale);
                if ((longDisplayName.startsWith("GMT+") && shortDisplayName.startsWith("GMT+"))
                        || (longDisplayName.startsWith("GMT-") && shortDisplayName.startsWith("GMT-"))) {
                    printText(locale, zdt, TextStyle.FULL, tz, tz.getID());
                    printText(locale, zdt, TextStyle.SHORT, tz, tz.getID());
                    continue;
                }
                printText(locale, zdt, TextStyle.FULL, tz,
                        tz.getDisplayName(isDST, TimeZone.LONG, locale));
                printText(locale, zdt, TextStyle.SHORT, tz,
                        tz.getDisplayName(isDST, TimeZone.SHORT, locale));
            }
        }
    }
}
 
Example #27
Source File: TestZoneTextPrinterParser.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void test_printText() {
    Random r = RandomFactory.getRandom();
    int N = 8;
    Locale[] locales = Locale.getAvailableLocales();
    Set<String> zids = ZoneRulesProvider.getAvailableZoneIds();
    ZonedDateTime zdt = ZonedDateTime.now();

    //System.out.printf("locale==%d, timezone=%d%n", locales.length, zids.size());
    while (N-- > 0) {
        zdt = zdt.withDayOfYear(r.nextInt(365) + 1)
                 .with(ChronoField.SECOND_OF_DAY, r.nextInt(86400));
        for (String zid : zids) {
            if (zid.equals("ROC") || zid.startsWith("Etc/GMT")) {
                continue;      // TBD: match jdk behavior?
            }
            zdt = zdt.withZoneSameLocal(ZoneId.of(zid));
            TimeZone tz = TimeZone.getTimeZone(zid);
            boolean isDST = tz.inDaylightTime(new Date(zdt.toInstant().toEpochMilli()));
            for (Locale locale : locales) {
                String longDisplayName = tz.getDisplayName(isDST, TimeZone.LONG, locale);
                String shortDisplayName = tz.getDisplayName(isDST, TimeZone.SHORT, locale);
                if ((longDisplayName.startsWith("GMT+") && shortDisplayName.startsWith("GMT+"))
                        || (longDisplayName.startsWith("GMT-") && shortDisplayName.startsWith("GMT-"))) {
                    printText(locale, zdt, TextStyle.FULL, tz, tz.getID());
                    printText(locale, zdt, TextStyle.SHORT, tz, tz.getID());
                    continue;
                }
                printText(locale, zdt, TextStyle.FULL, tz,
                        tz.getDisplayName(isDST, TimeZone.LONG, locale));
                printText(locale, zdt, TextStyle.SHORT, tz,
                        tz.getDisplayName(isDST, TimeZone.SHORT, locale));
            }
        }
    }
}
 
Example #28
Source File: Offsets.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
static Offsets init(String provider, String algorithm)
        throws NoSuchAlgorithmException, NoSuchProviderException,
        InvalidKeyException, SignatureException {
    // fill the cleartext data with random bytes
    byte[] cleartext = new byte[100];
    RandomFactory.getRandom().nextBytes(cleartext);

    // NONEwith requires input to be of 20 bytes
    int size = algorithm.contains("NONEwith") ? 20 : 100;

    // create signature instance
    Signature signature = Signature.getInstance(algorithm, provider);

    String keyAlgo;
    if (algorithm.contains("RSA")) {
        keyAlgo = "RSA";
    } else if (algorithm.contains("ECDSA")) {
        keyAlgo = "EC";
    } else if (algorithm.contains("DSA")) {
        keyAlgo = "DSA";
    } else {
        throw new RuntimeException("Test doesn't support this signature "
                + "algorithm: " + algorithm);
    }

    KeyPairGenerator kpg = KeyPairGenerator.getInstance(keyAlgo, provider);
    KeyPair kp = kpg.generateKeyPair();
    PublicKey pubkey = kp.getPublic();
    PrivateKey privkey = kp.getPrivate();

    return new Offsets(signature, pubkey, privkey, size, cleartext);
}
 
Example #29
Source File: TestZoneTextPrinterParser.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public void test_printText() {
    Random r = RandomFactory.getRandom();
    int N = 8;
    Locale[] locales = Locale.getAvailableLocales();
    Set<String> zids = ZoneRulesProvider.getAvailableZoneIds();
    ZonedDateTime zdt = ZonedDateTime.now();

    //System.out.printf("locale==%d, timezone=%d%n", locales.length, zids.size());
    while (N-- > 0) {
        zdt = zdt.withDayOfYear(r.nextInt(365) + 1)
                 .with(ChronoField.SECOND_OF_DAY, r.nextInt(86400));
        for (String zid : zids) {
            if (zid.equals("ROC") || zid.startsWith("Etc/GMT")) {
                continue;      // TBD: match jdk behavior?
            }
            zdt = zdt.withZoneSameLocal(ZoneId.of(zid));
            TimeZone tz = TimeZone.getTimeZone(zid);
            boolean isDST = tz.inDaylightTime(new Date(zdt.toInstant().toEpochMilli()));
            for (Locale locale : locales) {
                printText(locale, zdt, TextStyle.FULL, tz,
                        tz.getDisplayName(isDST, TimeZone.LONG, locale));
                printText(locale, zdt, TextStyle.SHORT, tz,
                        tz.getDisplayName(isDST, TimeZone.SHORT, locale));
            }
        }
    }
}
 
Example #30
Source File: Offsets.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
static Offsets init(String provider, String algorithm)
        throws NoSuchAlgorithmException, NoSuchProviderException,
        InvalidKeyException, SignatureException {
    // fill the cleartext data with random bytes
    byte[] cleartext = new byte[100];
    RandomFactory.getRandom().nextBytes(cleartext);

    // NONEwith requires input to be of 20 bytes
    int size = algorithm.contains("NONEwith") ? 20 : 100;

    // create signature instance
    Signature signature = Signature.getInstance(algorithm, provider);

    String keyAlgo;
    int keySize = 2048;
    if (algorithm.contains("RSA")) {
        keyAlgo = "RSA";
    } else if (algorithm.contains("ECDSA")) {
        keyAlgo = "EC";
        keySize = 256;
    } else if (algorithm.contains("DSA")) {
        keyAlgo = "DSA";
        if (algorithm.startsWith("SHAwith") ||
                algorithm.startsWith("SHA1with")) {
            keySize = 1024;
        }
    } else {
        throw new RuntimeException("Test doesn't support this signature "
                + "algorithm: " + algorithm);
    }

    KeyPairGenerator kpg = KeyPairGenerator.getInstance(keyAlgo, provider);
    kpg.initialize(keySize);
    KeyPair kp = kpg.generateKeyPair();
    PublicKey pubkey = kp.getPublic();
    PrivateKey privkey = kp.getPrivate();

    return new Offsets(signature, pubkey, privkey, size, cleartext);
}