1package eu.siacs.conversations.utils;
2
3import java.math.BigInteger;
4import java.nio.charset.Charset;
5import java.security.MessageDigest;
6import java.security.NoSuchAlgorithmException;
7import java.security.SecureRandom;
8
9import eu.siacs.conversations.entities.Account;
10
11import android.util.Base64;
12
13public class CryptoHelper {
14 public static final String FILETRANSFER = "?FILETRANSFERv1:";
15 final protected static char[] hexArray = "0123456789abcdef".toCharArray();
16 final protected static char[] vowels = "aeiou".toCharArray();
17 final protected static char[] consonants = "bcdfghjklmnpqrstvwxyz"
18 .toCharArray();
19
20 public static String bytesToHex(byte[] bytes) {
21 char[] hexChars = new char[bytes.length * 2];
22 for (int j = 0; j < bytes.length; j++) {
23 int v = bytes[j] & 0xFF;
24 hexChars[j * 2] = hexArray[v >>> 4];
25 hexChars[j * 2 + 1] = hexArray[v & 0x0F];
26 }
27 return new String(hexChars);
28 }
29
30 public static byte[] hexToBytes(String hexString) {
31 return new BigInteger(hexString, 16).toByteArray();
32 }
33
34 public static String saslPlain(String username, String password) {
35 String sasl = '\u0000' + username + '\u0000' + password;
36 return Base64.encodeToString(sasl.getBytes(Charset.defaultCharset()),
37 Base64.NO_WRAP);
38 }
39
40 private static byte[] concatenateByteArrays(byte[] a, byte[] b) {
41 byte[] result = new byte[a.length + b.length];
42 System.arraycopy(a, 0, result, 0, a.length);
43 System.arraycopy(b, 0, result, a.length, b.length);
44 return result;
45 }
46
47 public static String saslDigestMd5(Account account, String challenge, SecureRandom random) {
48 try {
49 String[] challengeParts = new String(Base64.decode(challenge,
50 Base64.DEFAULT)).split(",");
51 String nonce = "";
52 for (int i = 0; i < challengeParts.length; ++i) {
53 String[] parts = challengeParts[i].split("=");
54 if (parts[0].equals("nonce")) {
55 nonce = parts[1].replace("\"", "");
56 } else if (parts[0].equals("rspauth")) {
57 return null;
58 }
59 }
60 String digestUri = "xmpp/"+account.getServer();
61 String nonceCount = "00000001";
62 String x = account.getUsername() + ":" + account.getServer() + ":"
63 + account.getPassword();
64 MessageDigest md = MessageDigest.getInstance("MD5");
65 byte[] y = md
66 .digest(x.getBytes(Charset.defaultCharset()));
67 String cNonce = new BigInteger(100, random).toString(32);
68 byte[] a1 = concatenateByteArrays(y,(":"+nonce+":"+cNonce).getBytes(Charset.defaultCharset()));
69 String a2 = "AUTHENTICATE:"+digestUri;
70 String ha1 = bytesToHex(md.digest(a1));
71 String ha2 = bytesToHex(md.digest(a2.getBytes(Charset
72 .defaultCharset())));
73 String kd = ha1 + ":" + nonce + ":"+nonceCount+":" + cNonce + ":auth:"
74 + ha2;
75 String response = bytesToHex(md.digest(kd.getBytes(Charset
76 .defaultCharset())));
77 String saslString = "username=\"" + account.getUsername()
78 + "\",realm=\"" + account.getServer() + "\",nonce=\""
79 + nonce + "\",cnonce=\"" + cNonce
80 + "\",nc="+nonceCount+",qop=auth,digest-uri=\""+digestUri+"\",response=" + response
81 + ",charset=utf-8";
82 return Base64.encodeToString(
83 saslString.getBytes(Charset.defaultCharset()),
84 Base64.NO_WRAP);
85 } catch (NoSuchAlgorithmException e) {
86 return null;
87 }
88 }
89
90 public static String randomMucName(SecureRandom random) {
91 return randomWord(3, random) + "." + randomWord(7, random);
92 }
93
94 protected static String randomWord(int lenght, SecureRandom random) {
95 StringBuilder builder = new StringBuilder(lenght);
96 for (int i = 0; i < lenght; ++i) {
97 if (i % 2 == 0) {
98 builder.append(consonants[random.nextInt(consonants.length)]);
99 } else {
100 builder.append(vowels[random.nextInt(vowels.length)]);
101 }
102 }
103 return builder.toString();
104 }
105}