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