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