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;
 13
 14public class CryptoHelper {
 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).toLowerCase();
 28	}
 29
 30	public static String saslPlain(String username, String password) {
 31		String sasl = '\u0000' + username + '\u0000' + password;
 32		return Base64.encodeToString(sasl.getBytes(Charset.defaultCharset()),
 33				Base64.NO_WRAP);
 34	}
 35
 36	private static byte[] concatenateByteArrays(byte[] a, byte[] b) {
 37	    byte[] result = new byte[a.length + b.length]; 
 38	    System.arraycopy(a, 0, result, 0, a.length); 
 39	    System.arraycopy(b, 0, result, a.length, b.length); 
 40	    return result;
 41	} 
 42	
 43	public static String saslDigestMd5(Account account, String challenge) {
 44		try {
 45			Random random = new SecureRandom();
 46			String[] challengeParts = new String(Base64.decode(challenge,
 47					Base64.DEFAULT)).split(",");
 48			String nonce = "";
 49			for (int i = 0; i < challengeParts.length; ++i) {
 50				String[] parts = challengeParts[i].split("=");
 51				if (parts[0].equals("nonce")) {
 52					nonce = parts[1].replace("\"", "");
 53				} else if (parts[0].equals("rspauth")) {
 54					return null;
 55				}
 56			}
 57			String digestUri = "xmpp/"+account.getServer();
 58			String nonceCount = "00000001";
 59			String x = account.getUsername() + ":" + account.getServer() + ":"
 60					+ account.getPassword();
 61			MessageDigest md = MessageDigest.getInstance("MD5");
 62			byte[] y = md
 63					.digest(x.getBytes(Charset.defaultCharset()));
 64			String cNonce = new BigInteger(100, random).toString(32);
 65			byte[] a1 = concatenateByteArrays(y,(":"+nonce+":"+cNonce).getBytes(Charset.defaultCharset()));
 66			String a2 = "AUTHENTICATE:"+digestUri;
 67			String ha1 = bytesToHex(md.digest(a1));
 68			String ha2 = bytesToHex(md.digest(a2.getBytes(Charset
 69					.defaultCharset())));
 70			String kd = ha1 + ":" + nonce + ":"+nonceCount+":" + cNonce + ":auth:"
 71					+ ha2;
 72			String response = bytesToHex(md.digest(kd.getBytes(Charset
 73					.defaultCharset())));
 74			String saslString = "username=\"" + account.getUsername()
 75					+ "\",realm=\"" + account.getServer() + "\",nonce=\""
 76					+ nonce + "\",cnonce=\"" + cNonce
 77					+ "\",nc="+nonceCount+",qop=auth,digest-uri=\""+digestUri+"\",response=" + response
 78					+ ",charset=utf-8";
 79			return Base64.encodeToString(
 80					saslString.getBytes(Charset.defaultCharset()),
 81					Base64.NO_WRAP);
 82		} catch (NoSuchAlgorithmException e) {
 83			return null;
 84		}
 85	}
 86
 87	public static String randomMucName() {
 88		Random random = new SecureRandom();
 89		return randomWord(3, random) + "." + randomWord(7, random);
 90	}
 91
 92	protected static String randomWord(int lenght, Random random) {
 93		StringBuilder builder = new StringBuilder(lenght);
 94		for (int i = 0; i < lenght; ++i) {
 95			if (i % 2 == 0) {
 96				builder.append(consonants[random.nextInt(consonants.length)]);
 97			} else {
 98				builder.append(vowels[random.nextInt(vowels.length)]);
 99			}
100		}
101		return builder.toString();
102	}
103}