Conversations/src/eu/siacs/conversations/utils/CryptoHelper.java

35 lines
1.1 KiB
Java
Raw Normal View History

2014-03-07 14:24:33 +01:00
package eu.siacs.conversations.utils;
2014-03-09 12:58:29 +01:00
import android.util.Base64;
2014-03-07 14:24:33 +01:00
public class CryptoHelper {
final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
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) {
byte[] userBytes = username.getBytes();
int userLenght = userBytes.length;
byte[] passwordBytes = password.getBytes();
byte[] saslBytes = new byte[userBytes.length+passwordBytes.length+2];
saslBytes[0] = 0x0;
for(int i = 1; i < saslBytes.length; ++i) {
if (i<=userLenght) {
saslBytes[i] = userBytes[i-1];
} else if (i==userLenght+1) {
saslBytes[i] = 0x0;
} else {
saslBytes[i] = passwordBytes[i-(userLenght+2)];
}
}
return Base64.encodeToString(saslBytes, Base64.DEFAULT);
}
2014-03-07 14:24:33 +01:00
}