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