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.Arrays;
  9
 10import eu.siacs.conversations.entities.Account;
 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		byte[] array = new BigInteger(hexString, 16).toByteArray();
 32		if (array[0] == 0) {
 33			array = Arrays.copyOfRange(array, 1, array.length);
 34		}
 35		return array;
 36	}
 37
 38	public static String saslPlain(String username, String password) {
 39		String sasl = '\u0000' + username + '\u0000' + password;
 40		return Base64.encodeToString(sasl.getBytes(Charset.defaultCharset()),
 41				Base64.NO_WRAP);
 42	}
 43
 44	private static byte[] concatenateByteArrays(byte[] a, byte[] b) {
 45	    byte[] result = new byte[a.length + b.length]; 
 46	    System.arraycopy(a, 0, result, 0, a.length); 
 47	    System.arraycopy(b, 0, result, a.length, b.length); 
 48	    return result;
 49	} 
 50	
 51	public static String saslDigestMd5(Account account, String challenge, SecureRandom random) {
 52		try {
 53			String[] challengeParts = new String(Base64.decode(challenge,
 54					Base64.DEFAULT)).split(",");
 55			String nonce = "";
 56			for (int i = 0; i < challengeParts.length; ++i) {
 57				String[] parts = challengeParts[i].split("=");
 58				if (parts[0].equals("nonce")) {
 59					nonce = parts[1].replace("\"", "");
 60				} else if (parts[0].equals("rspauth")) {
 61					return null;
 62				}
 63			}
 64			String digestUri = "xmpp/"+account.getServer();
 65			String nonceCount = "00000001";
 66			String x = account.getUsername() + ":" + account.getServer() + ":"
 67					+ account.getPassword();
 68			MessageDigest md = MessageDigest.getInstance("MD5");
 69			byte[] y = md
 70					.digest(x.getBytes(Charset.defaultCharset()));
 71			String cNonce = new BigInteger(100, random).toString(32);
 72			byte[] a1 = concatenateByteArrays(y,(":"+nonce+":"+cNonce).getBytes(Charset.defaultCharset()));
 73			String a2 = "AUTHENTICATE:"+digestUri;
 74			String ha1 = bytesToHex(md.digest(a1));
 75			String ha2 = bytesToHex(md.digest(a2.getBytes(Charset
 76					.defaultCharset())));
 77			String kd = ha1 + ":" + nonce + ":"+nonceCount+":" + cNonce + ":auth:"
 78					+ ha2;
 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			return Base64.encodeToString(
 87					saslString.getBytes(Charset.defaultCharset()),
 88					Base64.NO_WRAP);
 89		} catch (NoSuchAlgorithmException e) {
 90			return null;
 91		}
 92	}
 93
 94	public static String randomMucName(SecureRandom random) {
 95		return randomWord(3, random) + "." + randomWord(7, random);
 96	}
 97
 98	protected static String randomWord(int lenght, SecureRandom random) {
 99		StringBuilder builder = new StringBuilder(lenght);
100		for (int i = 0; i < lenght; ++i) {
101			if (i % 2 == 0) {
102				builder.append(consonants[random.nextInt(consonants.length)]);
103			} else {
104				builder.append(vowels[random.nextInt(vowels.length)]);
105			}
106		}
107		return builder.toString();
108	}
109}