|
12 | 12 | * rights and limitations under the License.
|
13 | 13 | *
|
14 | 14 | * Copyright (C) 2006 Ola Bini <[email protected]>
|
15 |
| - * |
| 15 | + * |
16 | 16 | * Alternatively, the contents of this file may be used under the terms of
|
17 | 17 | * either of the GNU General Public License Version 2 or later (the "GPL"),
|
18 | 18 | * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
33 | 33 | * @author <a href="mailto:[email protected]">Ola Bini</a>
|
34 | 34 | */
|
35 | 35 | public class SimpleSecretKey implements SecretKey {
|
| 36 | + |
36 | 37 | private static final long serialVersionUID = 1L;
|
37 | 38 |
|
38 | 39 | private final String algorithm;
|
39 | 40 | private final byte[] value;
|
40 |
| - public SimpleSecretKey(String algorithm, byte[] value) { |
| 41 | + |
| 42 | + public SimpleSecretKey(final String algorithm, final byte[] value) { |
41 | 43 | this.algorithm = algorithm;
|
42 | 44 | this.value = value;
|
43 | 45 | }
|
| 46 | + |
| 47 | + public static SimpleSecretKey copy(String algorithm, final byte[] value) { |
| 48 | + return copy(algorithm, value, 0, value.length); |
| 49 | + } |
| 50 | + |
| 51 | + public static SimpleSecretKey copy(String algorithm, final byte[] value, int off, int len) { |
| 52 | + final byte[] val = new byte[len]; |
| 53 | + System.arraycopy(value, off, val, 0, len); |
| 54 | + return new SimpleSecretKey(algorithm, val); |
| 55 | + } |
| 56 | + |
44 | 57 | public String getAlgorithm() {
|
45 | 58 | return algorithm;
|
46 | 59 | }
|
| 60 | + |
47 | 61 | public byte[] getEncoded() {
|
48 | 62 | return value;
|
49 | 63 | }
|
| 64 | + |
50 | 65 | public String getFormat() {
|
51 | 66 | return "RAW";
|
52 |
| - } |
| 67 | + } |
| 68 | + |
| 69 | + public boolean equals(Object o) { |
| 70 | + if ( o instanceof SimpleSecretKey ) { |
| 71 | + byte[] ovalue = ((SimpleSecretKey) o).value; |
| 72 | + if ( value.length != ovalue.length ) return false; |
| 73 | + for ( int i = 0; i < value.length; i++ ) { |
| 74 | + if ( value[i] != ovalue[i] ) return false; |
| 75 | + } |
| 76 | + return algorithm.equals( ((SimpleSecretKey) o).algorithm ); |
| 77 | + } |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + public int hashCode() { |
| 82 | + int code = 0; |
| 83 | + for ( int i = 0; i < value.length; i++ ) { |
| 84 | + code ^= (value[i] & 0xff) << (i << 3 & 31); |
| 85 | + } |
| 86 | + return code ^ algorithm.hashCode(); |
| 87 | + } |
| 88 | + |
53 | 89 | }// SimpleSecretKey
|
0 commit comments