< prev index next >

test/jdk/com/sun/crypto/provider/Cipher/ChaCha20/ChaCha20KeyGeneratorTest.java

Print this page

13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test
26  * @bug 8153029
27  * @summary ChaCha20 key generator test.
28  * @library /test/lib
29  */
30 
31 import java.security.InvalidAlgorithmParameterException;
32 import java.security.InvalidParameterException;

33 
34 import javax.crypto.KeyGenerator;
35 import javax.crypto.SecretKey;
36 import javax.crypto.spec.ChaCha20ParameterSpec;
37 
38 import jdk.test.lib.Utils;
39 
40 public class ChaCha20KeyGeneratorTest {
41 
42     public static void main(String[] args) throws Exception {
43         KeyGenerator generator = KeyGenerator.getInstance("ChaCha20");
44 
45         try {
46             generator.init(new ChaCha20ParameterSpec(
47                     Utils.toByteArray("100000000000000000000000"), 0));
48             throw new RuntimeException(
49                     "ChaCha20 key generation should not consume AlgorithmParameterSpec");
50         } catch (InvalidAlgorithmParameterException e) {
51             System.out.println("Expected InvalidAlgorithmParameterException: "
52                     + e.getMessage());
53         }
54 
55         for (int keySize : new int[] { 32, 64, 128, 512, 1024 }) {
56             try {
57                 generator.init(keySize);
58                 throw new RuntimeException("The key size for ChaCha20 must be 256");
59             } catch (InvalidParameterException e) {
60                 System.out.println(
61                         "Expected InvalidParameterException: " + keySize);
62             }
63         }
64 
65         generator.init(256);
66         SecretKey key = generator.generateKey();
67         byte[] keyValue = key.getEncoded();
68         System.out.println("Key: " + Utils.toHexString(keyValue));
69         if (keyValue.length != 32) {
70             throw new RuntimeException("The size of generated key must be 256");
71         }
72     }
73 }

13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test
26  * @bug 8153029
27  * @summary ChaCha20 key generator test.
28  * @library /test/lib
29  */
30 
31 import java.security.InvalidAlgorithmParameterException;
32 import java.security.InvalidParameterException;
33 import java.util.Hex;
34 
35 import javax.crypto.KeyGenerator;
36 import javax.crypto.SecretKey;
37 import javax.crypto.spec.ChaCha20ParameterSpec;
38 


39 public class ChaCha20KeyGeneratorTest {
40 
41     public static void main(String[] args) throws Exception {
42         KeyGenerator generator = KeyGenerator.getInstance("ChaCha20");
43 
44         try {
45             generator.init(new ChaCha20ParameterSpec(
46                     Hex.decoder().decode("100000000000000000000000"), 0));
47             throw new RuntimeException(
48                     "ChaCha20 key generation should not consume AlgorithmParameterSpec");
49         } catch (InvalidAlgorithmParameterException e) {
50             System.out.println("Expected InvalidAlgorithmParameterException: "
51                     + e.getMessage());
52         }
53 
54         for (int keySize : new int[] { 32, 64, 128, 512, 1024 }) {
55             try {
56                 generator.init(keySize);
57                 throw new RuntimeException("The key size for ChaCha20 must be 256");
58             } catch (InvalidParameterException e) {
59                 System.out.println(
60                         "Expected InvalidParameterException: " + keySize);
61             }
62         }
63 
64         generator.init(256);
65         SecretKey key = generator.generateKey();
66         byte[] keyValue = key.getEncoded();
67         System.out.println("Key: " + Hex.encoder().encode(keyValue));
68         if (keyValue.length != 32) {
69             throw new RuntimeException("The size of generated key must be 256");
70         }
71     }
72 }
< prev index next >