CryptoHelper.java

 1package eu.siacs.conversations.utils;
 2
 3import java.nio.charset.Charset;
 4import java.security.SecureRandom;
 5import java.util.Random;
 6
 7import android.util.Base64;
 8
 9public class CryptoHelper {
10	final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
11	final protected static char[] vowels = "aeiou".toCharArray();
12	final protected static char[] consonants ="bcdfghjklmnpqrstvwxyz".toCharArray();
13	public static String bytesToHex(byte[] bytes) {
14	    char[] hexChars = new char[bytes.length * 2];
15	    for ( int j = 0; j < bytes.length; j++ ) {
16	        int v = bytes[j] & 0xFF;
17	        hexChars[j * 2] = hexArray[v >>> 4];
18	        hexChars[j * 2 + 1] = hexArray[v & 0x0F];
19	    }
20	    return new String(hexChars);
21	}
22	public static String saslPlain(String username, String password) {
23		String sasl = '\u0000'+username + '\u0000' + password;
24		return Base64.encodeToString(sasl.getBytes(Charset.defaultCharset()),Base64.NO_WRAP);
25	}
26	
27	public static String randomMucName() {
28		Random random = new SecureRandom();
29		return randomWord(3,random)+"."+randomWord(7,random);
30	}
31	
32	protected static String randomWord(int lenght,Random random) {
33		StringBuilder builder = new StringBuilder(lenght);
34		for(int i = 0; i < lenght; ++i) {
35			if (i % 2 == 0) {
36				builder.append(consonants[random.nextInt(consonants.length)]);
37			} else {
38				builder.append(vowels[random.nextInt(vowels.length)]);
39			}
40		}
41		return builder.toString();
42	}
43}