CryptoHelper.java

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