diff options
| author | Bananeweizen <bananeweizen@gmx.de> | 2013-08-28 20:55:07 +0200 |
|---|---|---|
| committer | Bananeweizen <bananeweizen@gmx.de> | 2013-08-28 20:55:07 +0200 |
| commit | 82e732957b6973ca491d53a8ebf4779ebf199be8 (patch) | |
| tree | 1010f97b03ad61f3cc4d9515e08fbfce9053b951 | |
| parent | 44696cc0297906bba2bf2b26b2db86196a1855fc (diff) | |
| download | cgeo-82e732957b6973ca491d53a8ebf4779ebf199be8.zip cgeo-82e732957b6973ca491d53a8ebf4779ebf199be8.tar.gz cgeo-82e732957b6973ca491d53a8ebf4779ebf199be8.tar.bz2 | |
findbugs cleanup: default encoding
| -rw-r--r-- | main/src/cgeo/geocaching/utils/CryptUtils.java | 37 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/utils/Log.java | 7 | ||||
| -rw-r--r-- | tests/src/cgeo/geocaching/utils/CryptUtilsTest.java | 12 |
3 files changed, 39 insertions, 17 deletions
diff --git a/main/src/cgeo/geocaching/utils/CryptUtils.java b/main/src/cgeo/geocaching/utils/CryptUtils.java index 9d95c68..d98585a 100644 --- a/main/src/cgeo/geocaching/utils/CryptUtils.java +++ b/main/src/cgeo/geocaching/utils/CryptUtils.java @@ -1,12 +1,17 @@ package cgeo.geocaching.utils; +import org.apache.commons.lang3.CharEncoding; +import org.apache.commons.lang3.StringUtils; + import android.text.Spannable; import android.text.SpannableStringBuilder; +import java.io.UnsupportedEncodingException; import java.math.BigInteger; import java.security.GeneralSecurityException; import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; @@ -63,7 +68,7 @@ public final class CryptUtils { public static String rot13(String text) { if (text == null) { - return ""; + return StringUtils.EMPTY; } final StringBuilder result = new StringBuilder(); Rot13Encryption rot13 = new Rot13Encryption(); @@ -77,43 +82,45 @@ public final class CryptUtils { } public static String md5(String text) { - String hashed = ""; - try { final MessageDigest digest = MessageDigest.getInstance("MD5"); - digest.update(text.getBytes(), 0, text.length()); - hashed = new BigInteger(1, digest.digest()).toString(16); - } catch (Exception e) { + digest.update(text.getBytes(CharEncoding.UTF_8), 0, text.length()); + return new BigInteger(1, digest.digest()).toString(16); + } catch (NoSuchAlgorithmException e) { + Log.e("CryptUtils.md5", e); + } catch (UnsupportedEncodingException e) { Log.e("CryptUtils.md5", e); } - return hashed; + return StringUtils.EMPTY; } public static String sha1(String text) { - String hashed = ""; - try { final MessageDigest digest = MessageDigest.getInstance("SHA-1"); - digest.update(text.getBytes(), 0, text.length()); - hashed = new BigInteger(1, digest.digest()).toString(16); - } catch (Exception e) { + digest.update(text.getBytes(CharEncoding.UTF_8), 0, text.length()); + return new BigInteger(1, digest.digest()).toString(16); + } catch (NoSuchAlgorithmException e) { + Log.e("CryptUtils.sha1", e); + } catch (UnsupportedEncodingException e) { Log.e("CryptUtils.sha1", e); } - return hashed; + return StringUtils.EMPTY; } public static byte[] hashHmac(String text, String salt) { byte[] macBytes = {}; try { - final SecretKeySpec secretKeySpec = new SecretKeySpec(salt.getBytes(), "HmacSHA1"); + final SecretKeySpec secretKeySpec = new SecretKeySpec(salt.getBytes(CharEncoding.UTF_8), "HmacSHA1"); final Mac mac = Mac.getInstance("HmacSHA1"); mac.init(secretKeySpec); - macBytes = mac.doFinal(text.getBytes()); + macBytes = mac.doFinal(text.getBytes(CharEncoding.UTF_8)); } catch (GeneralSecurityException e) { Log.e("CryptUtils.hashHmac", e); + } catch (UnsupportedEncodingException e) { + Log.e("CryptUtils.hashHmac", e); } return macBytes; diff --git a/main/src/cgeo/geocaching/utils/Log.java b/main/src/cgeo/geocaching/utils/Log.java index aa8dbd1..1ade2f9 100644 --- a/main/src/cgeo/geocaching/utils/Log.java +++ b/main/src/cgeo/geocaching/utils/Log.java @@ -1,11 +1,14 @@ package cgeo.geocaching.utils; +import org.apache.commons.lang3.CharEncoding; + import android.os.Environment; import java.io.BufferedWriter; import java.io.File; -import java.io.FileWriter; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.OutputStreamWriter; import java.io.Writer; public final class Log { @@ -107,7 +110,7 @@ public final class Log { } Writer writer = null; try { - writer = new BufferedWriter(new FileWriter(file, true)); + writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true), CharEncoding.UTF_8)); writer.write(msg); } catch (final IOException e) { Log.e("logToFile: cannot write to " + file, e); diff --git a/tests/src/cgeo/geocaching/utils/CryptUtilsTest.java b/tests/src/cgeo/geocaching/utils/CryptUtilsTest.java index e727747..9264d29 100644 --- a/tests/src/cgeo/geocaching/utils/CryptUtilsTest.java +++ b/tests/src/cgeo/geocaching/utils/CryptUtilsTest.java @@ -23,4 +23,16 @@ public class CryptUtilsTest extends TestCase { public static void testIssue1902() { assertEquals("ƖƖlƖƖƖƖ", CryptUtils.rot13("ƖƖyƖƖƖƖ")); } + + public static void testSha1() { + assertEquals("da39a3ee5e6b4b0d3255bfef95601890afd80709", CryptUtils.sha1("")); + // expected value taken from debugger. should assure every developer uses UTF-8 + assertEquals("cf2f343f59cea81afc0a5a566cb138ba349c548f", CryptUtils.sha1("äöü")); + } + + public static void testMd5() { + assertEquals("d41d8cd98f00b204e9800998ecf8427e", CryptUtils.md5("")); + // expected value taken from debugger. should assure every developer uses UTF-8 + assertEquals("a7f4e3ec08f09be2ef7ecb4eea5f8981", CryptUtils.md5("äöü")); + } } |
