test/jdk/sun/security/rsa/TestKeyPairGeneratorInit.java
author valeriep
Wed, 07 Nov 2018 01:04:26 +0000
changeset 52430 4ee78b5583f9
permissions -rw-r--r--
8211049: Second parameter of "initialize" method is not used Summary: Use the specified random object instead of system default Reviewed-by: weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52430
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
     1
/*
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
     4
 *
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
     7
 * published by the Free Software Foundation.
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
     8
 *
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    13
 * accompanied this code).
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    14
 *
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    18
 *
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    21
 * questions.
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    22
 */
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    23
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    24
/**
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    25
 * @test
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    26
 * @bug     8211049
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    27
 * @summary make sure the supplied SecureRandom object is used
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    28
 */
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    29
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    30
import java.security.*;
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    31
import java.security.interfaces.*;
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    32
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    33
public class TestKeyPairGeneratorInit {
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    34
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    35
    private static class MySecureRandom extends SecureRandom {
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    36
        boolean isUsed = false;
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    37
        public MySecureRandom() {
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    38
            super();
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    39
        }
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    40
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    41
        public void nextBytes(byte[] bytes) {
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    42
            isUsed = true;
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    43
            super.nextBytes(bytes);
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    44
        }
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    45
    }
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    46
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    47
    public static void main(String[] args) throws Exception {
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    48
        KeyPairGenerator kpg =
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    49
            KeyPairGenerator.getInstance("RSA", "SunRsaSign");
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    50
        MySecureRandom rnd = new MySecureRandom();
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    51
        kpg.initialize(2048, rnd);
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    52
        System.out.println("Generate keypair then check");
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    53
        KeyPair kp = kpg.generateKeyPair();
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    54
        if (!rnd.isUsed) {
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    55
            throw new RuntimeException("ERROR: Supplied random not used");
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    56
        } else {
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    57
            System.out.println("=> Test passed");
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    58
        }
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    59
    }
4ee78b5583f9 8211049: Second parameter of "initialize" method is not used
valeriep
parents:
diff changeset
    60
}