CryptoHelper.java

 1package eu.siacs.conversations.utils;
 2
 3import android.util.Base64;
 4
 5public class CryptoHelper {
 6	final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
 7	public static String bytesToHex(byte[] bytes) {
 8	    char[] hexChars = new char[bytes.length * 2];
 9	    for ( int j = 0; j < bytes.length; j++ ) {
10	        int v = bytes[j] & 0xFF;
11	        hexChars[j * 2] = hexArray[v >>> 4];
12	        hexChars[j * 2 + 1] = hexArray[v & 0x0F];
13	    }
14	    return new String(hexChars);
15	}
16	public static String saslPlain(String username, String password) {
17		byte[] userBytes = username.getBytes();
18		int userLenght = userBytes.length;
19		byte[] passwordBytes = password.getBytes();
20		byte[] saslBytes = new byte[userBytes.length+passwordBytes.length+2];
21		saslBytes[0] = 0x0;
22		for(int i = 1; i < saslBytes.length; ++i) {
23			if (i<=userLenght) {
24				saslBytes[i] = userBytes[i-1];
25			} else if (i==userLenght+1) {
26				saslBytes[i] = 0x0;
27			} else {
28				saslBytes[i] = passwordBytes[i-(userLenght+2)];
29			}
30		}
31		
32		return Base64.encodeToString(saslBytes, Base64.DEFAULT);
33	}
34}