diff options
author | Sam Whited <sam@samwhited.com> | 2014-11-12 15:35:44 -0500 |
---|---|---|
committer | Sam Whited <sam@samwhited.com> | 2014-11-14 18:00:12 -0500 |
commit | 0e550789d372a1a83caa432e93a4f969a0607c9a (patch) | |
tree | 126ddf717b9a3d1c4f36e904c658341fad5d5177 /src/main/java/eu/siacs/conversations/utils | |
parent | c61120bfc416676147998a435674bf1ac1587359 (diff) |
Add SCRAM-SHA1 support
Factor out GS2 tokanization into own class
Add authentication exception class
Fixes #71
Diffstat (limited to 'src/main/java/eu/siacs/conversations/utils')
-rw-r--r-- | src/main/java/eu/siacs/conversations/utils/CryptoHelper.java | 31 |
1 files changed, 29 insertions, 2 deletions
diff --git a/src/main/java/eu/siacs/conversations/utils/CryptoHelper.java b/src/main/java/eu/siacs/conversations/utils/CryptoHelper.java index 11042d07..bcc54a26 100644 --- a/src/main/java/eu/siacs/conversations/utils/CryptoHelper.java +++ b/src/main/java/eu/siacs/conversations/utils/CryptoHelper.java @@ -1,13 +1,14 @@ package eu.siacs.conversations.utils; import java.security.SecureRandom; +import java.text.Normalizer; public class CryptoHelper { public static final String FILETRANSFER = "?FILETRANSFERv1:"; final protected static char[] hexArray = "0123456789abcdef".toCharArray(); final protected static char[] vowels = "aeiou".toCharArray(); - final protected static char[] consonants = "bcdfghjklmnpqrstvwxyz" - .toCharArray(); + final protected static char[] consonants = "bcdfghjklmnpqrstvwxyz".toCharArray(); + final public static byte[] ONE = new byte[] { 0, 0, 0, 1 }; public static String bytesToHex(byte[] bytes) { char[] hexChars = new char[bytes.length * 2]; @@ -51,4 +52,30 @@ public class CryptoHelper { } return builder.toString(); } + + /** + * Escapes usernames or passwords for SASL. + */ + public static String saslEscape(final String s) { + final StringBuilder sb = new StringBuilder((int) (s.length() * 1.1)); + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + switch (c) { + case ',': + sb.append("=2C"); + break; + case '=': + sb.append("=3D"); + break; + default: + sb.append(c); + break; + } + } + return sb.toString(); + } + + public static String saslPrep(final String s) { + return saslEscape(Normalizer.normalize(s, Normalizer.Form.NFKC)); + } } |