Skip to content

Commit 240918c

Browse files
committed
handle SimpleSecretKey equality and copy helpers
1 parent cd5c95b commit 240918c

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

src/main/java/org/jruby/ext/openssl/SimpleSecretKey.java

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* rights and limitations under the License.
1313
*
1414
* Copyright (C) 2006 Ola Bini <[email protected]>
15-
*
15+
*
1616
* Alternatively, the contents of this file may be used under the terms of
1717
* either of the GNU General Public License Version 2 or later (the "GPL"),
1818
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
@@ -33,21 +33,57 @@
3333
* @author <a href="mailto:[email protected]">Ola Bini</a>
3434
*/
3535
public class SimpleSecretKey implements SecretKey {
36+
3637
private static final long serialVersionUID = 1L;
3738

3839
private final String algorithm;
3940
private final byte[] value;
40-
public SimpleSecretKey(String algorithm, byte[] value) {
41+
42+
public SimpleSecretKey(final String algorithm, final byte[] value) {
4143
this.algorithm = algorithm;
4244
this.value = value;
4345
}
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+
4457
public String getAlgorithm() {
4558
return algorithm;
4659
}
60+
4761
public byte[] getEncoded() {
4862
return value;
4963
}
64+
5065
public String getFormat() {
5166
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+
5389
}// SimpleSecretKey

0 commit comments

Comments
 (0)