Skip to content

Commit 553c77b

Browse files
committed
organizes some methods and store demo data in class PersonData
'test' folder inside main/resources is now is called 'demo'. Added a JSON file with same user data encoded inside CommandLine, because I plan to inject this data from it in future commits.
1 parent 550c5c3 commit 553c77b

File tree

5 files changed

+135
-70
lines changed

5 files changed

+135
-70
lines changed

src/main/java/com/brunoarruda/hyperdcpabe/CommandLine.java

Lines changed: 99 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ public static void init(String[] args) {
8484
client = new Client(networkURL, adminName, adminEmail, adminPrivateKey);
8585
}
8686

87-
// user commands
87+
/*
88+
* USER MANAGEMENT USERS COMMANDS
89+
*/
90+
8891
public static void createUser(String[] args){
8992
String name = args[1];
9093
String email = args[2];
@@ -110,7 +113,9 @@ public static void load(String[] args){
110113
client.setActiveUser(args[1]);
111114
}
112115

113-
// DCPABE commands
116+
/*
117+
* DCPABE COMMANDS
118+
*/
114119

115120
public static void createAttributes(String[] args){
116121
String[] attributes = new String[args.length - 1];
@@ -139,7 +144,9 @@ public static void decrypt(String[] args){
139144

140145
}
141146

142-
// blockchain commands
147+
/*
148+
* BLOCKCHAIN COMMANDS
149+
*/
143150

144151
public static void requestAttributes(String[] args){
145152
String[] attributes = new String[args.length - 2];
@@ -171,7 +178,9 @@ public static void publish(String[] args){
171178
}
172179
}
173180

174-
// integrated blockchain / server commands
181+
/*
182+
* NETWORK (BLOCKCHAIN / FILE SERVER) COMMANDS
183+
*/
175184
public static void send(String[] args){
176185
if (args[1].equals("attributes")) {
177186
for (int i = 2; i < args.length; i++) {
@@ -192,15 +201,14 @@ public static void getRecordings(String[] args){
192201
client.getRecordings(args[1], recordings);
193202
}
194203

195-
// demonstration command
204+
/*
205+
* DEMONSTRATION OF USE CASES
206+
*/
196207
public static void milestone(String[] args){
197-
// parsing necessary to navigate through milestone function
198-
String[] numberSplit = args[1].split("\\.");
199-
int[] choices = new int[numberSplit.length];
200-
for (int i = 0; i < choices.length; i++) {
201-
choices[i] = Integer.parseInt(numberSplit[i]);
202-
}
203-
runMilestone(choices);
208+
String[] choice = args[1].split("\\.");
209+
int scenario = Integer.parseInt(choice[0]);
210+
int subScenario = (choice.length == 2 ? Integer.parseInt(choice[0]) : 0);
211+
runMilestone(scenario, subScenario);
204212
}
205213

206214
public static void help(String[] args){
@@ -267,75 +275,96 @@ public static void runCommand(String[] args) {
267275
client.decrypt(args[1]);
268276
break;
269277
default:
270-
System.out.println("Comando não reconhecido: " + String.join(" ", args));
278+
System.out.println("Command is not valid: " + String.join(" ", args));
271279
}
272280
}
273281

274-
public static void runMilestone(int[] choice) {
282+
public static void runMilestone(int scenario, int subScenario) {
283+
if ((scenario > 2) ||
284+
(scenario == 1 && subScenario != 0) ||
285+
(scenario == 2 && (subScenario < 1 || subScenario > 3))) {
286+
String subScenario_ = (subScenario == 0 ? "" : "." + subScenario);
287+
System.out.println(String.format("Milestone invalid: %s%s", scenario, subScenario_));
288+
System.exit(-1);
289+
}
290+
291+
PersonData admin = new PersonData(
292+
"admin",
293+
"0xFae373E0BFfaE794fA818D749D6da38D4f7cA986",
294+
"e4d8c81796894ea5bf202e3a3204948dddd62f4d709c278bf8096898957be241"
295+
);
296+
PersonData crm = new PersonData(
297+
"crm",
298+
"0xFB7EAfB7fBdaA775d0D52fAaEBC525C1cE173EE0",
299+
"e15b910f8c61580befebecff2d79abf38998035cbc317400a96c4736a424f6dc"
300+
);
301+
PersonData alice = new PersonData(
302+
"Alice",
303+
"0xb038476875480BCE0D0FCf0991B4BB108A3FCB47",
304+
"4237a475aa6579f2a0fc85d90cbcda1fad3db70391315a6c37b51de3a8cb503a"
305+
);
306+
PersonData bob = new PersonData(
307+
"Bob",
308+
"0xF7908374b1a445cCf65F729887dbB695c918BEfc",
309+
"ab0439882857ffb5859c1a3a6bf40a6848daeaab6605c873c3e425de53c2c4ab"
310+
);
311+
275312
/**
276313
* Milestone 1 cenário: novo prontuário Cliente 1 java usa o código do ABE (cria
277314
* usuário, obtém atributo1, envia prontuário1 - i.e., pdf1 encriptado) Cliente
278315
* 2 java usa o código do ABE (supondo que possui o atributo1, obtém prontuário1
279316
* codificado e o decodifica)
280317
*/
281318
String args;
282-
if (choice[0] == 1) {
319+
if (scenario == 1) {
283320
// pre-setup to deliver file to be encrypted to user folder
284-
String path = "data/client/Alice-0xb038476875480BCE0D0FCf0991B4BB108A3FCB47/";
321+
String path = String.format("data/client/%s/", alice.gid);
285322
new File(path).mkdirs();
286-
getFileFromResources(path, "test/lorem_ipsum.md", "lorem_ipsum.md");
323+
getFileFromResources(path, "demo/lorem_ipsum.md", "lorem_ipsum.md");
287324

288325
// admin inicia o sistema e cria os contratos
289-
args = "--init http://127.0.0.1:7545 admin [email protected] ";
290-
args = args + "e4d8c81796894ea5bf202e3a3204948dddd62f4d709c278bf8096898957be241";
326+
args = String.format("--init http://127.0.0.1:7545 %s %s %s", admin.name, admin.email, admin.pKey);
291327
init(args.split(" "));
292328

293329
// certificador cria perfil e atributo, e os publica
294-
args = "--create-user CRM [email protected] ";
295-
args = args + "e15b910f8c61580befebecff2d79abf38998035cbc317400a96c4736a424f6dc";
296-
runCommand(args.split(" "));
330+
runCommand(String.format("--create-user %s %s %s ", crm.name, crm.email, crm.pKey).split(" "));
297331
runCommand("--create-certifier".split(" "));
298332
runCommand("--create-attributes atributo1 atributo2 atributo3".split(" "));
299333
runCommand("--publish user certifier attributes".split(" "));
300334

301335
// usuário 1 - Bob, cria perfil e solicita concessão do atributo 1 (chave pessoal ABE)
302-
args = "--create-user Bob [email protected] ";
303-
args = args + "ab0439882857ffb5859c1a3a6bf40a6848daeaab6605c873c3e425de53c2c4ab";
304-
runCommand(args.split(" "));
336+
runCommand(String.format("--create-user %s %s %s ", bob.name, bob.email, bob.pKey).split(" "));
305337
runCommand("--publish user".split(" "));
306-
runCommand("--load Bob-0xF7908374b1a445cCf65F729887dbB695c918BEfc".split(" "));
307-
runCommand("--request-attribute CRM-0xFB7EAfB7fBdaA775d0D52fAaEBC525C1cE173EE0 atributo1".split(" "));
338+
runCommand(String.format("--request-attribute %s atributo1", crm.gid).split(" "));
308339

309340
// usuário 2 - Alice, cria perfil, recebe chaves públicas e criptografa um documento
310-
args = "--create-user Alice [email protected] ";
311-
args = args + "4237a475aa6579f2a0fc85d90cbcda1fad3db70391315a6c37b51de3a8cb503a";
312-
runCommand(args.split(" "));
341+
runCommand(String.format("--create-user %s %s %s ", alice.name, alice.email, alice.pKey).split(" "));
313342
runCommand("--publish user".split(" "));
314-
runCommand("--load Alice-0xb038476875480BCE0D0FCf0991B4BB108A3FCB47".split(" "));
315-
runCommand("--get-attributes CRM-0xFB7EAfB7fBdaA775d0D52fAaEBC525C1cE173EE0 atributo1".split(" "));
316-
runCommand("--encrypt lorem_ipsum.md atributo1 CRM-0xFB7EAfB7fBdaA775d0D52fAaEBC525C1cE173EE0".split(" "));
343+
runCommand(String.format("--get-attributes %s atributo1", crm.gid).split(" "));
344+
runCommand(String.format("--encrypt lorem_ipsum.md atributo1 %s", crm.gid).split(" "));
317345
runCommand("--send lorem_ipsum.md".split(" "));
318346

319347
// certificador recebe requisição de atributo e o concede ao Bob
320-
runCommand("--load CRM-0xFB7EAfB7fBdaA775d0D52fAaEBC525C1cE173EE0".split(" "));
348+
runCommand(String.format("--load %s", crm.gid).split(" "));
321349
runCommand("--check-requests pending".split(" "));
322-
runCommand("--yield-attributes Bob-0xF7908374b1a445cCf65F729887dbB695c918BEfc 0".split(" "));
323-
runCommand("--send attributes Bob-0xF7908374b1a445cCf65F729887dbB695c918BEfc".split(" "));
350+
runCommand(String.format("--yield-attributes %s 0", bob.gid).split(" "));
351+
runCommand(String.format("--send attributes %s", bob.gid).split(" "));
324352

325353
// usuário 1 - Bob, de posse do atributo, o descriptografa
326-
runCommand("--load Bob-0xF7908374b1a445cCf65F729887dbB695c918BEfc".split(" "));
354+
runCommand(String.format("--load %s", bob.gid).split(" "));
327355
runCommand("--check-requests ok".split(" "));
328356
runCommand("--check-requests download".split(" "));
329-
runCommand("--get-recordings Alice-0xb038476875480BCE0D0FCf0991B4BB108A3FCB47 lorem_ipsum.md".split(" "));
357+
runCommand(String.format("--get-recordings %s lorem_ipsum.md", alice.gid).split(" "));
330358
runCommand("--decrypt lorem_ipsum.md".split(" "));
331359
}
332360

333-
if (choice[0] == 2) {
361+
if (scenario == 2) {
334362
// pre-setup to deliver file to be encrypted to user folder
335-
String path = "data/client/Alice-0xb038476875480BCE0D0FCf0991B4BB108A3FCB47/";
363+
String path = String.format("data/client/%s/", alice.gid);
336364
new File(path).mkdirs();
337-
getFileFromResources(path, "test/lorem_ipsum2.md", "lorem_ipsum2.md");
365+
getFileFromResources(path, "demo/lorem_ipsum2.md", "lorem_ipsum2.md");
338366

367+
runMilestone(1, 0);
339368
/**
340369
* cenário: novo prontuário
341370
* Cliente 1 java (usuário já criado) obtém novos atributos: atributo2 e
@@ -344,15 +373,13 @@ public static void runMilestone(int[] choice) {
344373
* java (só com atributo1) tenta mas não consegue obter o prontuário2 (nem
345374
* decodificá-lo) pois não tem atributos.
346375
*/
347-
if (choice[1] == 1) {
348-
runMilestone(new int[]{1});
349-
runCommand("--load Alice-0xb038476875480BCE0D0FCf0991B4BB108A3FCB47".split(" "));
350-
runCommand("--get-attributes CRM-0xFB7EAfB7fBdaA775d0D52fAaEBC525C1cE173EE0 atributo2 atributo3".split(" "));
351-
String[] specialArgs = {"--encrypt", "lorem_ipsum2.md", "and atributo2 atributo3", "CRM-0xFB7EAfB7fBdaA775d0D52fAaEBC525C1cE173EE0"};
352-
runCommand(specialArgs);
376+
if (subScenario == 1) {
377+
runCommand(String.format("--load %s", alice.gid).split(" "));
378+
runCommand(String.format("--get-attributes %s atributo2 atributo3", crm.gid).split(" "));
379+
runCommand(String.format("--encrypt lorem_ipsum2.md \"and atributo2 atributo3\" %s", crm.gid).split(" "));
353380
runCommand("--send lorem_ipsum2.md".split(" "));
354-
runCommand("--load Bob-0xF7908374b1a445cCf65F729887dbB695c918BEfc".split(" "));
355-
runCommand("--get-recordings Alice-0xb038476875480BCE0D0FCf0991B4BB108A3FCB47 lorem_ipsum2.md".split(" "));
381+
runCommand(String.format("--load %s", bob.gid).split(" "));
382+
runCommand(String.format("--get-recordings %s lorem_ipsum2.md", alice.gid).split(" "));
356383
runCommand("--decrypt lorem_ipsum2.md".split(" "));
357384
}
358385
/**
@@ -362,39 +389,30 @@ public static void runMilestone(int[] choice) {
362389
* Cliente 2 java obtém prontuário1 codificado e o decodifica (pois possui
363390
* atributo1 do milestone1).
364391
*/
365-
if (choice[1] == 2) {
366-
runMilestone(new int[] { 1 });
367-
getFileFromResources(path, "test/lorem_ipsum-edit.md", "lorem_ipsum.md");
368-
runCommand("--load Alice-0xb038476875480BCE0D0FCf0991B4BB108A3FCB47".split(" "));
369-
runCommand("--encrypt lorem_ipsum.md atributo1 CRM-0xFB7EAfB7fBdaA775d0D52fAaEBC525C1cE173EE0".split(" "));
392+
if (subScenario == 2) {
393+
getFileFromResources(path, "demo/lorem_ipsum-edit.md", "lorem_ipsum.md");
394+
runCommand(String.format("--load %s", alice.gid).split(" "));
395+
runCommand(String.format("--encrypt lorem_ipsum.md atributo1 %s", crm.gid).split(" "));
370396
runCommand("--send lorem_ipsum.md".split(" "));
371-
runCommand("--load Bob-0xF7908374b1a445cCf65F729887dbB695c918BEfc".split(" "));
372-
runCommand("--get-recordings Alice-0xb038476875480BCE0D0FCf0991B4BB108A3FCB47 lorem_ipsum.md".split(" "));
397+
runCommand(String.format("--load %s", bob.gid).split(" "));
398+
runCommand(String.format("--get-recordings %s lorem_ipsum.md", alice.gid).split(" "));
373399
runCommand("--decrypt lorem_ipsum.md".split(" "));
374400
}
375401
/*
376402
* Cliente 1 java procura prontuario1 e atualiza: a) atributo1 por atributo1 AND
377403
* atributo2 e b) dataEnvio. Cliente 2 java (só com atributo1) tenta mas não
378404
* consegue obter o prontuário1 (nem decodificá-lo) pois não tem atributo2.
379405
*/
380-
if (choice[1] == 3) {
381-
runMilestone(new int[] { 1 });
382-
runCommand("--load Alice-0xb038476875480BCE0D0FCf0991B4BB108A3FCB47".split(" "));
383-
runCommand("--get-attributes CRM-0xFB7EAfB7fBdaA775d0D52fAaEBC525C1cE173EE0 atributo1".split(" "));
384-
String[] encryptArgs = { "--encrypt", "lorem_ipsum.md", "and atributo1 atributo2",
385-
"CRM-0xFB7EAfB7fBdaA775d0D52fAaEBC525C1cE173EE0" };
386-
runCommand(encryptArgs);
406+
if (subScenario == 3) {
407+
runCommand(String.format("--load %s", alice.gid).split(" "));
408+
runCommand(String.format("--get-attributes %s atributo1", crm.gid).split(" "));
409+
runCommand(String.format("--encrypt lorem_ipsum \"and atributo1 atributo2\" %s", crm.gid).split(" "));
387410
runCommand("--send lorem_ipsum.md".split(" "));
388-
runCommand("--load Bob-0xF7908374b1a445cCf65F729887dbB695c918BEfc".split(" "));
389-
runCommand("--get-recordings Alice-0xb038476875480BCE0D0FCf0991B4BB108A3FCB47 lorem_ipsum.md".split(" "));
411+
runCommand(String.format("--load %s", bob.gid).split(" "));
412+
runCommand(String.format("--get-recordings %s lorem_ipsum.md", alice.gid).split(" "));
390413
runCommand("--decrypt lorem_ipsum.md".split(" "));
391414
}
392415
}
393-
394-
if (choice[0] > 2) {
395-
System.out.println("Milestone not recognized.");
396-
System.exit(-1);
397-
}
398416
}
399417

400418
private static void getFileFromResources(String path, String inputFileName, String outputFileName) {
@@ -418,4 +436,15 @@ private static void getFileFromResources(String path, String inputFileName, Stri
418436
}
419437
}
420438
}
439+
private static class PersonData {
440+
public String name, email, address, gid, pKey;
441+
442+
public PersonData(String name, String address, String privateKey) {
443+
this.name = name;
444+
this.email = name.toLowerCase() + "@email.com";
445+
this.address = address;
446+
this.gid = name + "-" + this.address;
447+
this.pKey = privateKey;
448+
}
449+
}
421450
}

src/main/resources/demo/data.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"users":[
3+
{
4+
"name":"admin",
5+
"email":"[email protected]",
6+
"address":"0xFae373E0BFfaE794fA818D749D6da38D4f7cA986",
7+
"GID":"admin-0xFae373E0BFfaE794fA818D749D6da38D4f7cA986",
8+
"publicKey":"",
9+
"privateKey":"e4d8c81796894ea5bf202e3a3204948dddd62f4d709c278bf8096898957be241"
10+
},
11+
{
12+
"name":"CRM",
13+
"email":"[email protected]",
14+
"address":"0xFB7EAfB7fBdaA775d0D52fAaEBC525C1cE173EE0",
15+
"GID":"CRM-0xFB7EAfB7fBdaA775d0D52fAaEBC525C1cE173EE0",
16+
"publicKey":"",
17+
"privateKey":"e15b910f8c61580befebecff2d79abf38998035cbc317400a96c4736a424f6dc"
18+
},
19+
{
20+
"name":"Alice",
21+
"email":"[email protected]",
22+
"address":"0xb038476875480BCE0D0FCf0991B4BB108A3FCB47",
23+
"GID":"Alice-0xb038476875480BCE0D0FCf0991B4BB108A3FCB47",
24+
"publicKey":"",
25+
"privateKey":"4237a475aa6579f2a0fc85d90cbcda1fad3db70391315a6c37b51de3a8cb503a"
26+
},
27+
{
28+
"name":"Bob",
29+
"email":"[email protected]",
30+
"address":"0xF7908374b1a445cCf65F729887dbB695c918BEfc",
31+
"GID":"Bob-0xF7908374b1a445cCf65F729887dbB695c918BEfc",
32+
"publicKey":"",
33+
"privateKey":"ab0439882857ffb5859c1a3a6bf40a6848daeaab6605c873c3e425de53c2c4ab"
34+
}
35+
]
36+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)