- binary to hex representation (aka Hex class of commons codec)
- base64 implementation
- Tiny Encryption Algorithm (XXTEA)
- ASCII to binary conversion
Most of existing coding JavaScript library works based on String, but String encoding may vary between Java and JavaScript. Integers cannot. On Java-side, the library can work with bytes, integers and most of all NIO buffers.
Here is an excerpt of what you can do with this library. The result is the same in Java, Groovy and JavaScript providing the same input.
Java / Groovy
// ascii <-> binary conversion
String str = 'abcdefghijklmnopqrstuvwxyz'
IntBuffer buffer = ASCII.toIntBuffer(str)
assert str == ASCII.fromIntBuffer(buffer)
// hex representation
String str = '0123456789ABCDEF'
IntBuffer buffer = HEX.decodeHexAsInts(str)
assert str == HEX.encodeHexString(buffer)
// Base64
String str = Base64.encodeBase64String(buffer)
ByteBuffer buffer = Base64.decodeBase64(str)
// TEA key generation
Random r = new SecureRandom()
IntBuffer key = IntBuffer.wrap([
r.nextInt(),
r.nextInt(),
r.nextInt(),
r.nextInt()] as int[]) //128-bits keys
// TEA encryption
String data = "...." // > 128-bits data
IntBuffer buffer = ASCII.toIntBuffer(data)
XXTEA.encryptInPlace(buffer, key)
String b64 = Base64.encodeBase64String(buffer)
// TEA decryption
buffer = Base64.decodeBase64(b64).asIntBuffer()
XXTEA.decryptInPlace(buffer, key)
String clear_text = ASCII.fromIntBuffer(buffer)
JavaScript
// ascii <-> binary conversion
var str = 'abcdefghijklmnopqrstuvwxyz';
var int_array = ovea.crypto.ascii.toInts(str);
str == ovea.crypto.ascii.fromInts(int_array);
// hex representation
var str = '0123456789ABCDEF'v
var int_array = ovea.crypto.hex.decode(str);
str == ovea.crypto.hex.encode(int_array);
// Base64
var str = ovea.crypto.base64.encode(buffer);
var int_array = ovea.crypto.base64.decode(str);
// TEA key generation
var key = [0, 1, 2, 3] // 128-bits key
// TEA encryption
var data = "...." // > 128-bits data
var buffer = ovea.crypto.ascii.toInts(data);
ovea.crypto.xxtea.encryptInPlace(buffer, key);
var b64 = ovea.crypto.base64.encode(buffer);
// TEA decryption
var buffer = ovea.crypto.base64.decode(b64);
ovea.crypto.xxtea.decryptInPlace(buffer, key);
var clear_text = ovea.crypto.ascii.fromInts(buffer);
ovea.crypto.xxtea.decryptInPlace(buffer, key);
var clear_text = ovea.crypto.ascii.fromInts(buffer);
The project java-js-crypto is available on Ovea's GitHub. The license is Apache 2 and libraries are deployed in Maven2 Central Repository at:
http://repo1.maven.org/maven2/com/ovea/ovea-crypto/
