1 /* |
1 /* |
2 * Copyright (c) 2003, 2009, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
103 byte[] b = new byte[(keySize + 7) >> 3]; |
103 byte[] b = new byte[(keySize + 7) >> 3]; |
104 random.nextBytes(b); |
104 random.nextBytes(b); |
105 return new SecretKeySpec(b, name); |
105 return new SecretKeySpec(b, name); |
106 } |
106 } |
107 |
107 |
108 // nested static class for the HmacSHA256 key generator |
108 // nested static classes for the HmacSHA-2 family of key generator |
109 public static final class HmacSHA256KG extends KeyGeneratorSpi { |
109 abstract static class HmacSHA2KG extends KeyGeneratorSpi { |
110 private final KeyGeneratorCore core; |
110 private final KeyGeneratorCore core; |
111 public HmacSHA256KG() { |
111 protected HmacSHA2KG(String algoName, int len) { |
112 core = new KeyGeneratorCore("HmacSHA256", 256); |
112 core = new KeyGeneratorCore(algoName, len); |
113 } |
113 } |
114 protected void engineInit(SecureRandom random) { |
114 protected void engineInit(SecureRandom random) { |
115 core.implInit(random); |
115 core.implInit(random); |
116 } |
116 } |
117 protected void engineInit(AlgorithmParameterSpec params, |
117 protected void engineInit(AlgorithmParameterSpec params, |
122 core.implInit(keySize, random); |
122 core.implInit(keySize, random); |
123 } |
123 } |
124 protected SecretKey engineGenerateKey() { |
124 protected SecretKey engineGenerateKey() { |
125 return core.implGenerateKey(); |
125 return core.implGenerateKey(); |
126 } |
126 } |
127 } |
127 |
128 |
128 public static final class SHA224 extends HmacSHA2KG { |
129 // nested static class for the HmacSHA384 key generator |
129 public SHA224() { |
130 public static final class HmacSHA384KG extends KeyGeneratorSpi { |
130 super("HmacSHA224", 224); |
131 private final KeyGeneratorCore core; |
131 } |
132 public HmacSHA384KG() { |
132 } |
133 core = new KeyGeneratorCore("HmacSHA384", 384); |
133 public static final class SHA256 extends HmacSHA2KG { |
134 } |
134 public SHA256() { |
135 protected void engineInit(SecureRandom random) { |
135 super("HmacSHA256", 256); |
136 core.implInit(random); |
136 } |
137 } |
137 } |
138 protected void engineInit(AlgorithmParameterSpec params, |
138 public static final class SHA384 extends HmacSHA2KG { |
139 SecureRandom random) throws InvalidAlgorithmParameterException { |
139 public SHA384() { |
140 core.implInit(params, random); |
140 super("HmacSHA384", 384); |
141 } |
141 } |
142 protected void engineInit(int keySize, SecureRandom random) { |
142 } |
143 core.implInit(keySize, random); |
143 public static final class SHA512 extends HmacSHA2KG { |
144 } |
144 public SHA512() { |
145 protected SecretKey engineGenerateKey() { |
145 super("HmacSHA512", 512); |
146 return core.implGenerateKey(); |
146 } |
147 } |
|
148 } |
|
149 |
|
150 // nested static class for the HmacSHA384 key generator |
|
151 public static final class HmacSHA512KG extends KeyGeneratorSpi { |
|
152 private final KeyGeneratorCore core; |
|
153 public HmacSHA512KG() { |
|
154 core = new KeyGeneratorCore("HmacSHA512", 512); |
|
155 } |
|
156 protected void engineInit(SecureRandom random) { |
|
157 core.implInit(random); |
|
158 } |
|
159 protected void engineInit(AlgorithmParameterSpec params, |
|
160 SecureRandom random) throws InvalidAlgorithmParameterException { |
|
161 core.implInit(params, random); |
|
162 } |
|
163 protected void engineInit(int keySize, SecureRandom random) { |
|
164 core.implInit(keySize, random); |
|
165 } |
|
166 protected SecretKey engineGenerateKey() { |
|
167 return core.implGenerateKey(); |
|
168 } |
147 } |
169 } |
148 } |
170 |
149 |
171 // nested static class for the RC2 key generator |
150 // nested static class for the RC2 key generator |
172 public static final class RC2KeyGenerator extends KeyGeneratorSpi { |
151 public static final class RC2KeyGenerator extends KeyGeneratorSpi { |