151 lines
4.6 KiB
Java
151 lines
4.6 KiB
Java
|
|
package com.sequencelogic;
|
||
|
|
|
||
|
|
import java.io.File;
|
||
|
|
import java.io.FileInputStream;
|
||
|
|
import java.io.IOException;
|
||
|
|
import java.io.InputStream;
|
||
|
|
import java.security.MessageDigest;
|
||
|
|
import java.security.NoSuchAlgorithmException;
|
||
|
|
|
||
|
|
import com.sequencelogic.ByteArr;
|
||
|
|
import com.sequencelogic.eyeinterface;
|
||
|
|
|
||
|
|
|
||
|
|
// Eye method wrapper class
|
||
|
|
public class EyeWrapper {
|
||
|
|
private static final char[] HexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
|
||
|
|
|
||
|
|
private static String digestName = "";
|
||
|
|
private static MessageDigest digest;
|
||
|
|
private static final int IONU_AES_BLOCK_LEN = 16;
|
||
|
|
private static final int IONU_AES_KEY_LEN = 32;
|
||
|
|
|
||
|
|
static {
|
||
|
|
}
|
||
|
|
|
||
|
|
public EyeWrapper() {
|
||
|
|
}
|
||
|
|
|
||
|
|
private static ByteArr BytesToByteArr (byte[] bytes)
|
||
|
|
{
|
||
|
|
ByteArr ba = new ByteArr(bytes.length);
|
||
|
|
for (int i = 0; i < bytes.length; ++i)
|
||
|
|
ba.setitem(i, bytes[i]);
|
||
|
|
return ba;
|
||
|
|
}
|
||
|
|
|
||
|
|
private static byte[] ByteArrToBytes (ByteArr ba, int len)
|
||
|
|
{
|
||
|
|
byte [] bytes = new byte[len];
|
||
|
|
for (int i = 0; i < len; ++i)
|
||
|
|
bytes[i] = (byte)ba.getitem(i);
|
||
|
|
return bytes;
|
||
|
|
}
|
||
|
|
private static void setdigest (String algo) {
|
||
|
|
if (digestName != algo) {
|
||
|
|
digestName = algo;
|
||
|
|
try {
|
||
|
|
digest = MessageDigest.getInstance (digestName);
|
||
|
|
}
|
||
|
|
catch (NoSuchAlgorithmException e) {
|
||
|
|
throw new RuntimeException ("Could not create an instance of " + digestName);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private static String digest (String string) {
|
||
|
|
digest.reset();
|
||
|
|
return encodeHex (digest.digest (string.getBytes()));
|
||
|
|
}
|
||
|
|
|
||
|
|
public static String md5 (String string) {
|
||
|
|
//setdigest ("md5");
|
||
|
|
//return digest (string);
|
||
|
|
|
||
|
|
ByteArr mdb = BytesToByteArr (string.getBytes());
|
||
|
|
return eyeinterface.ionu_digest_msg ("md5", mdb.cast(), string.length());
|
||
|
|
}
|
||
|
|
|
||
|
|
public static String sha1 (String string) {
|
||
|
|
setdigest ("sha1");
|
||
|
|
return digest (string);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static String md5file (String path) {
|
||
|
|
return eyeinterface.ionu_digest_file ("md5", path);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static byte[] symmetricEncryptBuf (String passphrase, byte[] data) {
|
||
|
|
ByteArr d = BytesToByteArr (data);
|
||
|
|
ByteArr e = new ByteArr (2048);
|
||
|
|
int elen = (int)eyeinterface.ionu_symmetric_encrypt_buffer_pass (passphrase, d.cast(), data.length, e.cast());
|
||
|
|
byte[] ebytes = ByteArrToBytes (e, elen);
|
||
|
|
d.delete();
|
||
|
|
e.delete();
|
||
|
|
return ebytes;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static byte[] symmetricDecryptBuf (String passphrase, byte[] data) {
|
||
|
|
ByteArr d = BytesToByteArr (data);
|
||
|
|
ByteArr e = new ByteArr (data.length + 16);
|
||
|
|
int elen = (int)eyeinterface.ionu_symmetric_decrypt_buffer_pass (passphrase, d.cast(), data.length, e.cast());
|
||
|
|
byte[] ebytes = ByteArrToBytes (e, elen);
|
||
|
|
d.delete();
|
||
|
|
e.delete();
|
||
|
|
return ebytes;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static byte[] asymmetricEncryptBuf (String publicpem, byte[] data) {
|
||
|
|
ByteArr d = BytesToByteArr (data);
|
||
|
|
ByteArr e = new ByteArr (2048);
|
||
|
|
int elen = (int)eyeinterface.ionu_public_key_encrypt (publicpem, d.cast(), data.length, e.cast());
|
||
|
|
byte[] ebytes = ByteArrToBytes (e, elen);
|
||
|
|
d.delete();
|
||
|
|
e.delete();
|
||
|
|
return ebytes;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static byte[] asymmetricDecryptBuf (String privatepem, byte[] data) {
|
||
|
|
ByteArr d = BytesToByteArr (data);
|
||
|
|
ByteArr e = new ByteArr (2048);
|
||
|
|
int elen = (int)eyeinterface.ionu_private_key_decrypt (privatepem, d.cast(), data.length, e.cast());
|
||
|
|
byte[] ebytes = ByteArrToBytes (e, elen);
|
||
|
|
d.delete();
|
||
|
|
e.delete();
|
||
|
|
return ebytes;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static byte[] asymmetricSignBuf (String privatepem, byte[] data) {
|
||
|
|
ByteArr d = BytesToByteArr (data);
|
||
|
|
ByteArr e = new ByteArr (2048);
|
||
|
|
int elen = (int)eyeinterface.ionu_private_key_encrypt (privatepem, d.cast(), data.length, e.cast());
|
||
|
|
byte[] ebytes = ByteArrToBytes (e, elen);
|
||
|
|
d.delete();
|
||
|
|
e.delete();
|
||
|
|
return ebytes;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static byte[] asymmetricVerifyBuf (String publicpem, byte[] data) {
|
||
|
|
ByteArr d = BytesToByteArr (data);
|
||
|
|
ByteArr e = new ByteArr (2048);
|
||
|
|
int elen = (int)eyeinterface.ionu_public_key_decrypt (publicpem, d.cast(), data.length, e.cast());
|
||
|
|
byte[] ebytes = ByteArrToBytes (e, elen);
|
||
|
|
d.delete();
|
||
|
|
e.delete();
|
||
|
|
return ebytes;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
// Encode the digest as a hex encoded string
|
||
|
|
private static String encodeHex(byte[] data) {
|
||
|
|
int length = data.length;
|
||
|
|
char[] out = new char[length << 1];
|
||
|
|
for (int i = 0, j = 0; i < length; i++) {
|
||
|
|
out[j++] = HexDigits[(0xF0 & data[i]) >>> 4];
|
||
|
|
out[j++] = HexDigits[0x0F & data[i]];
|
||
|
|
}
|
||
|
|
return new String (out);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|