diff options
Diffstat (limited to 'main/src/cgeo/geocaching/utils/CryptUtils.java')
| -rw-r--r-- | main/src/cgeo/geocaching/utils/CryptUtils.java | 62 |
1 files changed, 32 insertions, 30 deletions
diff --git a/main/src/cgeo/geocaching/utils/CryptUtils.java b/main/src/cgeo/geocaching/utils/CryptUtils.java index df2baa0..18a337d 100644 --- a/main/src/cgeo/geocaching/utils/CryptUtils.java +++ b/main/src/cgeo/geocaching/utils/CryptUtils.java @@ -12,6 +12,10 @@ import javax.crypto.spec.SecretKeySpec; public final class CryptUtils { + private CryptUtils() { + // utility class + } + private static char[] base64map1 = new char[64]; private static byte[] base64map2 = new byte[128]; @@ -37,28 +41,36 @@ public final class CryptUtils { } } + private static class Rot13Encryption { + private boolean plaintext = false; + + char getNextEncryptedCharacter(final char c) { + int result = c; + if (result == '[') { + plaintext = true; + } else if (result == ']') { + plaintext = false; + } else if (!plaintext) { + int capitalized = result & 32; + result &= ~capitalized; + result = ((result >= 'A') && (result <= 'Z') ? ((result - 'A' + 13) % 26 + 'A') : result) + | capitalized; + } + return (char) result; + } + } + public static String rot13(String text) { if (text == null) { return ""; } final StringBuilder result = new StringBuilder(); - // plaintext flag (do not convert) - boolean plaintext = false; + Rot13Encryption rot13 = new Rot13Encryption(); final int length = text.length(); for (int index = 0; index < length; index++) { - int c = text.charAt(index); - if (c == '[') { - plaintext = true; - } else if (c == ']') { - plaintext = false; - } else if (!plaintext) { - int capitalized = c & 32; - c &= ~capitalized; - c = ((c >= 'A') && (c <= 'Z') ? ((c - 'A' + 13) % 26 + 'A') : c) - | capitalized; - } - result.append((char) c); + char c = text.charAt(index); + result.append(rot13.getNextEncryptedCharacter(c)); } return result.toString(); } @@ -71,7 +83,7 @@ public final class CryptUtils { digest.update(text.getBytes(), 0, text.length()); hashed = new BigInteger(1, digest.digest()).toString(16); } catch (Exception e) { - Log.e("cgBase.md5", e); + Log.e("CryptUtils.md5", e); } return hashed; @@ -85,7 +97,7 @@ public final class CryptUtils { digest.update(text.getBytes(), 0, text.length()); hashed = new BigInteger(1, digest.digest()).toString(16); } catch (Exception e) { - Log.e("cgBase.sha1", e); + Log.e("CryptUtils.sha1", e); } return hashed; @@ -100,7 +112,7 @@ public final class CryptUtils { mac.init(secretKeySpec); macBytes = mac.doFinal(text.getBytes()); } catch (Exception e) { - Log.e("cgBase.hashHmac", e); + Log.e("CryptUtils.hashHmac", e); } return macBytes; @@ -111,22 +123,12 @@ public final class CryptUtils { // a SpannableStringBuilder instead of the pure text and we must replace each character inline. // Otherwise we loose all the images, colors and so on... final SpannableStringBuilder buffer = new SpannableStringBuilder(span); - boolean plaintext = false; + Rot13Encryption rot13 = new Rot13Encryption(); final int length = span.length(); for (int index = 0; index < length; index++) { - int c = span.charAt(index); - if (c == '[') { - plaintext = true; - } else if (c == ']') { - plaintext = false; - } else if (!plaintext) { - int capitalized = c & 32; - c &= ~capitalized; - c = ((c >= 'A') && (c <= 'Z') ? ((c - 'A' + 13) % 26 + 'A') : c) - | capitalized; - } - buffer.replace(index, index + 1, String.valueOf((char) c)); + char c = span.charAt(index); + buffer.replace(index, index + 1, String.valueOf(rot13.getNextEncryptedCharacter(c))); } return buffer; } |
