package com.zaranux.k2c;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import com.zaranux.crypto.abe.kp.ABE;
import com.zaranux.crypto.abe.kp.Entity;
import java.io.ByteArrayOutputStream;
import java.io.FileWriter;
import java.io.PrintWriter;
public class Test {
/**
* @param args
*/
public static void abe_init0() throws Exception
{
abe = new ABE();
FileOutputStream fos = new FileOutputStream("C:/abe");
abe.write(fos);
root = abe.getRootEntity();
FileOutputStream fos2 = new FileOutputStream("C:/abe_root");
root.write(fos2);
}
//ar = root.derive("(and l0=/ (< lr0 150) l1=a (< lr1 150))");
//aw = root.derive("(and w l0=/ l1=a)");
private static String getWriteExp(String path)
{
String[] pathComp = DataObject.getPathComps(path);
StringBuffer exp = new StringBuffer();
exp.append("(and w ");
for(int i =0 ; i <pathComp.length ; i++)
{
exp.append("l" + i + "=" + pathComp[i]+ " ");
}
exp.append(" )");
return exp.toString();
}
private static String getReadExp(String path)
{
String[] pathComp = DataObject.getPathComps(path);
StringBuffer exp = new StringBuffer();
exp.append("(and ");
for(int i =0 ; i <pathComp.length ; i++)
{
exp.append("l" + i + "=" + pathComp[i]+ " ");
exp.append("(< lr" + i + " 2 ) ");
}
exp.append(" )");
return exp.toString();
}
private static void createInitialDirectories() throws Exception
{
abe_init();
DataObject.init();
new DataObject("/",1,1,root).create();
new DataObject("/1",1,1,root).create();
new DataObject("/1/1",1,1,root).create();
new DataObject("/1/1/1",1,1,root).create();
new DataObject("/1/1/1/1",1,1,root).create();
new DataObject("/1/1/1/1/1",1,1,root).create();
new DataObject("/1/1/1/1/1/1",1,1,root).create();
new DataObject("/1/1/1/1/1/1/2",1,1,root).create();
new DataObject("/1/1/1/1/1/2",1,1,root).create();
new DataObject("/1/1/1/1/2",1,1,root).create();
new DataObject("/1/1/1/2",1,1,root).create();
new DataObject("/1/1/2",1,1,root).create();
new DataObject("/1/2",1,1,root).create();
new DataObject("/2",1,1,root).create();
}
public static byte[] getData(int e) throws Exception {
{
ByteArrayOutputStream b = new ByteArrayOutputStream();
b.write(1);
for(int i = 0 ; i<e ; i++)
{
b.write(b.toByteArray());
}
return b.toByteArray();
}
}
static PrintWriter out = null;
public static byte[] enc_temp = null;
public static void main(String[] args) throws Exception
{
out = new PrintWriter(new FileWriter("C:/temp/out.txt"));
try{
readSize();
//writeSize();
}
catch(Exception e)
{
System.out.println(e.getStackTrace() + "");
}finally
{
out.close();
}
// abe_init();
// DataObject.init();
// byte[] data = getData(10);
// new DataObject("/2",root).read();
// new DataObject("/1/2",root).read();
// new DataObject("/1/1/2",root).write(data);
// new DataObject("/1/1/2",root).read();
// new DataObject("/1/1/1/2",root).read();
}
public static void read() throws Exception {
//createInitialDirectories();
abe_init();
DataObject.init();
byte[] data = null; //getData(10);
long t1 , t2;
DataObject d;
String ePath = "/";
Entity e = null;
for(int j =0 ; j< 7; j++)
{
if(j==0)
e = root;
else
{
ePath += "/1";
e = root.derive(Test.getReadExp(ePath));
}
out.println( "" );
out.println( "" + ePath );
String dir = "/";
for(int i = 0; i< 7 ; i++)
{
if(i>=j)
{
d = new DataObject(dir +"2",e);
t1 = System.currentTimeMillis();
data = d.read();
System.out.println("" + data.length);
t2 = System.currentTimeMillis();
out.println(dir + "2\t" + (float)(t2-t1)/1000);
}else
{
out.println(dir + "2\t");
}
dir = "/1" + dir;
}
if(j==0)
ePath = "";
}
}
public static void readSize() throws Exception {
try{
abe_init();
DataObject.init();
for(int k=0; k < 11 ; k++)
{
//byte[] data = getData(10 + k);
String dir = "/";
Entity e = root;
for(int i = 0; i< 1 ; i++)
{
DataObject d = new DataObject(dir +"2_"+k,e);
long t1 = System.currentTimeMillis();
byte[] data = d.read();
long t2 = System.currentTimeMillis();
out.println(data.length + " \t " +dir + "2_" + k + "\t" + (float)(t2-t1)/1000);
dir = "/1" + dir;
}
}
}catch(Exception e)
{
System.out.println(e.getStackTrace() + "");
}finally
{
out.close();
}
}
public static void writeSize() throws Exception {
//createInitialDirectories();
abe_init();
DataObject.init();
for(int k=0; k < 11 ; k++)
{
byte[] data = getData(10 + k);
String dir = "/";
Entity e = root;
// for(int i = 0; i< 7 ; i++)
for(int i = 0; i< 1 ; i++)
{
DataObject d = new DataObject(dir +"2_"+k,e);
long t1 = System.currentTimeMillis();
d.write(data);
long t2 = System.currentTimeMillis();
out.println(dir + "2_" + k + "\t" + (float)(t2-t1)/1000);
dir = "/1" + dir;
}
}
}
public static void write() throws Exception {
//createInitialDirectories();
abe_init();
DataObject.init();
byte[] data = getData(10);
long t1 , t2;
DataObject d;
String ePath = "/";
Entity e = null;
for(int j =0 ; j< 4; j++)
{
if(j==0)
e = root;
else
{
ePath += "/1";
e = root.derive(Test.getWriteExp(ePath));
}
out.println( "" );
out.println( "" + ePath );
String dir = "/";
for(int i = 0; i< 7 ; i++)
{
if(i>=j)
{
d = new DataObject(dir +"2",e);
t1 = System.currentTimeMillis();
d.write(data);
t2 = System.currentTimeMillis();
out.println(dir + "2\t" + (float)(t2-t1)/1000);
}else
{
out.println(dir + "2\t");
}
dir = "/1" + dir;
}
if(j==0)
ePath = "";
}
}
private static ABE abe ;
private static Entity root ;
public static void abe_init() throws Exception
{
FileInputStream fis = new FileInputStream("C:/abe");
abe = new ABE(fis);
FileInputStream fis2 = new FileInputStream("C:/abe_root");
root = new Entity(abe, fis2);
}
public static void main2(String[] args) throws Exception {
abe_init();
//byte[] data0 = new byte[]{1,3,5,6};
//byte[] ciphertext = root.encrypt(data0, new String[]{"l0=/", "#lr0=2", "l1=a", "#lr1=3", "l2=b", "#lr2=18"});
//byte[] data = root.decrypt(ciphertext);
DataObject.init();
//ABE abe = new ABE();
//Entity root = abe.getRootEntity();
DataObject dobjw; // = new DataObject("/a/b",root);
// dobjw.list();
// dobjw.load();
// dobjw.revokeRead();
// dobjw.save();
Entity ar = null;
Entity aw = null;
try
{
ar = root.derive("(and l0=/ (< lr0 150) l1=a (< lr1 150))");
aw = root.derive("(and w l0=/ l1=a)");
}
catch(Exception e)
{
System.out.println("e:" + e);
}
dobjw = new DataObject("/a/b",aw);
//dobjw = new DataObject("/a/b",root,root);
dobjw.write("Test".getBytes());
DataObject dobjr = new DataObject("/a/b",ar);
byte[] d = dobjr.read();
if(d!= null)
{
String s = new String(d);
System.out.print("Read " + s);
}
else
{
System.out.print("unalbe to read !");
}
// dobj.load();
// dobj.revokeRead();
// dobj.save();
// dobj.list();
// DataObject[] dojts = dobj.getAnscesstors();
// for(DataObject d:dojts)
// {
// System.out.println(""+d);
// }
//dobj.write("Saman".getBytes());
// dobj.read();
}
}