From afac50e2a7bfaf70d53a8265194b5a5ca13034f8 Mon Sep 17 00:00:00 2001 From: Tarun Gupta Date: Sun, 26 Jul 2015 13:49:02 +0530 Subject: Remove unnecessary Crypto functions from StringCodecs. Remove SHA1 and HMACSHA1 from StringCodecs as they are already provided by CryptoProvider, and is equivalent to Swiften. License: This patch is BSD-licensed, see Documentation/Licenses/BSD-simplified.txt for details. Test-Information: Add tests for Base64 and PBKDF2, which passes. Change-Id: Ife05f185a10a79c9d69a12235f1b0397d022d992 diff --git a/src/com/isode/stroke/sasl/SCRAMSHA1ClientAuthenticator.java b/src/com/isode/stroke/sasl/SCRAMSHA1ClientAuthenticator.java index 9797e24..1d2b4d9 100644 --- a/src/com/isode/stroke/sasl/SCRAMSHA1ClientAuthenticator.java +++ b/src/com/isode/stroke/sasl/SCRAMSHA1ClientAuthenticator.java @@ -12,9 +12,7 @@ import com.isode.stroke.base.ByteArray; import com.isode.stroke.base.SafeByteArray; import com.ibm.icu.text.StringPrepParseException; import com.isode.stroke.stringcodecs.Base64; -import com.isode.stroke.stringcodecs.HMACSHA1; import com.isode.stroke.stringcodecs.PBKDF2; -import com.isode.stroke.stringcodecs.SHA1; import com.isode.stroke.idn.IDNConverter; import com.isode.stroke.crypto.CryptoProvider; import java.text.Normalizer; diff --git a/src/com/isode/stroke/stringcodecs/HMACSHA1.java b/src/com/isode/stroke/stringcodecs/HMACSHA1.java deleted file mode 100644 index 36f8f80..0000000 --- a/src/com/isode/stroke/stringcodecs/HMACSHA1.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (c) 2010, Isode Limited, London, England. - * All rights reserved. - */ -/* - * Copyright (c) 2010, Remko Tronçon. - * All rights reserved. - */ -package com.isode.stroke.stringcodecs; - -import com.isode.stroke.base.ByteArray; - -public class HMACSHA1 { - - private static final int B = 64; - - public static ByteArray getResult(ByteArray key, ByteArray data) { - assert key.getSize() <= B; - - /* And an assert that does something */ - if (key.getSize() > B) { - throw new IllegalStateException("Invalid key size."); - } - - // Create the padded key - ByteArray paddedKey = new ByteArray(key); - for (int i = key.getSize(); i < B; ++i) { - paddedKey.append((byte) 0x0); - } - - // Create the first value - ByteArray x = new ByteArray(paddedKey); - byte[] xInner = x.getData(); - for (int i = 0; i < xInner.length; ++i) { - xInner[i] ^= 0x36; - } - x = new ByteArray(xInner); - x.append(data); - - // Create the second value - ByteArray y = new ByteArray(paddedKey); - byte[] yInner = y.getData(); - for (int i = 0; i < yInner.length; ++i) { - yInner[i] ^= 0x5c; - } - y = new ByteArray(yInner); - y.append(SHA1.getHash(x)); - - return SHA1.getHash(y); - } -} diff --git a/src/com/isode/stroke/stringcodecs/SHA1.java b/src/com/isode/stroke/stringcodecs/SHA1.java deleted file mode 100644 index 9beb650..0000000 --- a/src/com/isode/stroke/stringcodecs/SHA1.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright (c) 2010, Isode Limited, London, England. - * All rights reserved. - */ -/* - * Copyright (c) 2010, Remko Tronçon. - * All rights reserved. - */ -package com.isode.stroke.stringcodecs; - -import com.isode.stroke.base.ByteArray; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; - -public class SHA1 { - - public static ByteArray getHash(ByteArray data) { - MessageDigest md; - try { - md = MessageDigest.getInstance("SHA-1"); - } catch (NoSuchAlgorithmException ex) { - throw new IllegalStateException("JRE doesn't have an SHA hash function", ex); - } - md.update(data.getData()); - return new ByteArray(md.digest()); - } -} diff --git a/src/com/isode/stroke/tls/Certificate.java b/src/com/isode/stroke/tls/Certificate.java index fdd64c0..3bed6d8 100644 --- a/src/com/isode/stroke/tls/Certificate.java +++ b/src/com/isode/stroke/tls/Certificate.java @@ -11,7 +11,6 @@ package com.isode.stroke.tls; import com.isode.stroke.base.ByteArray; import com.isode.stroke.crypto.CryptoProvider; import com.isode.stroke.stringcodecs.Hexify; -import com.isode.stroke.stringcodecs.SHA1; import java.util.List; public abstract class Certificate { diff --git a/test/com/isode/stroke/stringcodecs/Base64Test.java b/test/com/isode/stroke/stringcodecs/Base64Test.java new file mode 100644 index 0000000..7365955 --- /dev/null +++ b/test/com/isode/stroke/stringcodecs/Base64Test.java @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2010 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.stringcodecs; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import org.junit.Test; +import org.junit.Before; +import com.isode.stroke.base.ByteArray; +import com.isode.stroke.stringcodecs.Base64; + +public class Base64Test { + + @Test + public void testEncodeDecodeAllChars() { + ByteArray input = new ByteArray(); + for (int i = 0; i < 255; ++i) { + char c = (char)i; + input.append((byte)c); + } + String result = Base64.encode(input); + + assertEquals(("AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+"), result); + assertEquals(input, Base64.decode(result)); + } + + @Test + public void testEncodeDecodeOneBytePadding() { + ByteArray input = new ByteArray("ABCDE"); + + String result = Base64.encode(input); + + assertEquals(("QUJDREU="), result); + assertEquals(input, Base64.decode(result)); + } + + @Test + public void testEncodeDecodeTwoBytesPadding() { + ByteArray input = new ByteArray("ABCD"); + + String result = Base64.encode(input); + + assertEquals(("QUJDRA=="), result); + assertEquals(input, Base64.decode(result)); + } + + @Test + public void testEncode_NoData() { + String result = (Base64.encode(new ByteArray())); + assertEquals((""), result); + } + + @Test + public void testDecode_NoData() { + ByteArray result = (Base64.decode("")); + assertEquals(new ByteArray(), result); + } +} \ No newline at end of file diff --git a/test/com/isode/stroke/stringcodecs/HMACSHA1Test.java b/test/com/isode/stroke/stringcodecs/HMACSHA1Test.java deleted file mode 100644 index dedcd7c..0000000 --- a/test/com/isode/stroke/stringcodecs/HMACSHA1Test.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) 2010, Isode Limited, London, England. - * All rights reserved. - */ -/* - * Copyright (c) 2010, Remko Tron?on. - * All rights reserved. - */ - -package com.isode.stroke.stringcodecs; - -import com.isode.stroke.base.ByteArray; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import static org.junit.Assert.*; - -public class HMACSHA1Test { - - private ByteArray cast(int[] source) { - byte[] result = new byte[source.length]; - for (int i = 0; i < source.length; i++) { - result[i] = (byte)source[i]; - } - return new ByteArray(result); - } - - @Test - public void testGetResult() { - ByteArray result = HMACSHA1.getResult(new ByteArray("foo"), new ByteArray("foobar")); - assertEquals(cast(new int[]{0xa4, 0xee, 0xba, 0x8e, 0x63, 0x3d, 0x77, 0x88, 0x69, 0xf5, 0x68, 0xd0, 0x5a, 0x1b, 0x3d, 0xc7, 0x2b, 0xfd, 0x4, 0xdd}), result); - } -} diff --git a/test/com/isode/stroke/stringcodecs/PBKDF2Test.java b/test/com/isode/stroke/stringcodecs/PBKDF2Test.java new file mode 100644 index 0000000..e76ca5e --- /dev/null +++ b/test/com/isode/stroke/stringcodecs/PBKDF2Test.java @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2010-2013 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.stringcodecs; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import org.junit.Test; +import org.junit.Before; +import com.isode.stroke.base.ByteArray; +import com.isode.stroke.base.SafeByteArray; +import com.isode.stroke.stringcodecs.PBKDF2; +import com.isode.stroke.crypto.CryptoProvider; +import com.isode.stroke.crypto.JavaCryptoProvider; + +public class PBKDF2Test { + + private CryptoProvider crypto; + + @Before + public void setUp() { + crypto = new JavaCryptoProvider(); + } + + @Test + public void testGetResult_I1() { + ByteArray result = PBKDF2.encode(new SafeByteArray("password"), new ByteArray("salt"), 1, crypto); + + assertEquals(new ByteArray("0c60c80f961f0e71f3a9b524af6012062fe037a6"), new ByteArray(Hexify.hexify(result))); + } + + @Test + public void testGetResult_I2() { + ByteArray result = PBKDF2.encode(new SafeByteArray("password"), new ByteArray("salt"), 2, crypto); + + assertEquals(new ByteArray("ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957"), new ByteArray(Hexify.hexify(result))); + } + + @Test + public void testGetResult_I4096() { + ByteArray result = PBKDF2.encode(new SafeByteArray("password"), new ByteArray("salt"), 4096, crypto); + + assertEquals(new ByteArray("4b007901b765489abead49d926f721d065a429c1"), new ByteArray(Hexify.hexify(result))); + } +} diff --git a/test/com/isode/stroke/stringcodecs/SHA1Test.java b/test/com/isode/stroke/stringcodecs/SHA1Test.java deleted file mode 100644 index c8c8a9b..0000000 --- a/test/com/isode/stroke/stringcodecs/SHA1Test.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (c) 2010, Isode Limited, London, England. - * All rights reserved. - */ -/* - * Copyright (c) 2010, Remko Tron?on. - * All rights reserved. - */ -package com.isode.stroke.stringcodecs; - -import com.isode.stroke.base.ByteArray; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import static org.junit.Assert.*; - -public class SHA1Test { - - public SHA1Test() { - } - - @Test - public void testGetHash() { - ByteArray result = new ByteArray(SHA1.getHash(new ByteArray("client/pc//Exodus 0.9.1