2014-03-07 14:24:33 +01:00
|
|
|
package eu.siacs.conversations.utils;
|
|
|
|
|
2014-03-28 14:48:11 +01:00
|
|
|
import java.nio.charset.Charset;
|
2014-03-15 15:13:35 +01:00
|
|
|
import java.security.SecureRandom;
|
|
|
|
import java.util.Random;
|
|
|
|
|
2014-03-09 12:58:29 +01:00
|
|
|
import android.util.Base64;
|
2014-03-28 14:48:11 +01:00
|
|
|
import android.util.Log;
|
2014-03-09 12:58:29 +01:00
|
|
|
|
2014-03-07 14:24:33 +01:00
|
|
|
public class CryptoHelper {
|
|
|
|
final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
|
2014-03-15 15:13:35 +01:00
|
|
|
final protected static char[] vowels = "aeiou".toCharArray();
|
|
|
|
final protected static char[] consonants ="bcdfghjklmnpqrstvwxyz".toCharArray();
|
2014-03-07 14:24:33 +01:00
|
|
|
public static String bytesToHex(byte[] bytes) {
|
|
|
|
char[] hexChars = new char[bytes.length * 2];
|
|
|
|
for ( int j = 0; j < bytes.length; j++ ) {
|
|
|
|
int v = bytes[j] & 0xFF;
|
|
|
|
hexChars[j * 2] = hexArray[v >>> 4];
|
|
|
|
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
|
|
|
|
}
|
|
|
|
return new String(hexChars);
|
|
|
|
}
|
2014-03-09 12:58:29 +01:00
|
|
|
public static String saslPlain(String username, String password) {
|
2014-03-28 14:48:11 +01:00
|
|
|
String sasl = '\u0000'+username + '\u0000' + password;
|
|
|
|
return Base64.encodeToString(sasl.getBytes(Charset.defaultCharset()),Base64.DEFAULT);
|
2014-03-09 12:58:29 +01:00
|
|
|
}
|
2014-03-15 15:13:35 +01:00
|
|
|
|
|
|
|
public static String randomMucName() {
|
|
|
|
Random random = new SecureRandom();
|
|
|
|
return randomWord(3,random)+"."+randomWord(7,random);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static String randomWord(int lenght,Random random) {
|
|
|
|
StringBuilder builder = new StringBuilder(lenght);
|
|
|
|
for(int i = 0; i < lenght; ++i) {
|
|
|
|
if (i % 2 == 0) {
|
|
|
|
builder.append(consonants[random.nextInt(consonants.length)]);
|
|
|
|
} else {
|
|
|
|
builder.append(vowels[random.nextInt(vowels.length)]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return builder.toString();
|
|
|
|
}
|
2014-03-07 14:24:33 +01:00
|
|
|
}
|