Skip to content

Commit

Permalink
Issue #797: fixed reuse of SHA message digests by Guava.
Browse files Browse the repository at this point in the history
	Change on 2016/09/28 by tball <tball@google.com>

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=134560133
  • Loading branch information
tomball committed Sep 28, 2016
1 parent 9d2924f commit e4fbaac
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.google.j2objc.security;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.MessageDigest;

/*-[
Expand Down Expand Up @@ -54,6 +55,21 @@ protected void engineReset() {
buffer.reset();
}

public Object clone() throws CloneNotSupportedException {
IosSHAMessageDigest obj = (IosSHAMessageDigest) super.clone();
// ByteArrayOutputStreams are not cloneable, so copy it.
obj.buffer = new ByteArrayOutputStream();
if (buffer.size() > 0) {
try {
obj.buffer.write(buffer.toByteArray());
} catch (IOException e) {
// Should never happen.
throw new AssertionError(e);
}
}
return obj;
}

public static class SHA1 extends IosSHAMessageDigest {

public SHA1() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.j2objc.security;

import java.security.MessageDigest;
import junit.framework.TestCase;


/**
* Unit tests for {@link IosSHAMessageDigest}.
*
* @author Tom Ball
*/
public class IosSHAMessageDigestTest extends TestCase {

// Issue #797: cloned digests returned different hash values.
public void testDigestCloning() throws Exception {
MessageDigest digest = MessageDigest.getInstance("SHA-1");
String hash1 = hash("foo", (MessageDigest) digest.clone());
String hash2 = hash("foo", (MessageDigest) digest.clone());
assertEquals(hash1, hash2);
}

private String hash(String input, MessageDigest digest) throws Exception {
byte[] bytes = input.getBytes("UTF-8");
digest.update(bytes);
byte[] hash = digest.digest();
StringBuilder sb = new StringBuilder(2 * bytes.length);
for (byte b : hash) {
sb.append(hexDigits[(b >> 4) & 0xf]).append(hexDigits[b & 0xf]);
}
return sb.toString();
}

private static final char[] hexDigits = "0123456789abcdef".toCharArray();
}
1 change: 1 addition & 0 deletions jre_emul/test_sources.mk
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ TEST_SOURCES := \
com/google/j2objc/ThrowableTest.java \
com/google/j2objc/net/NSErrorExceptionTest.java \
com/google/j2objc/security/IosSecureRandomImplTest.java \
com/google/j2objc/security/IosSHAMessageDigestTest.java \
com/google/j2objc/util/NativeTimeZoneTest.java \
dalvik/system/JniTest.java \
java/lang/SystemTest.java \
Expand Down

0 comments on commit e4fbaac

Please sign in to comment.