1 #!/usr/bin/env dub 2 /+ dub.sdl: 3 name "bench_jwtlited" 4 targetType "executable" 5 6 configuration "openssl" { 7 targetName "bench_jwtlited_openssl" 8 dependency "jwtlited:openssl" path="../" 9 versions "USE_OPENSSL" 10 } 11 12 configuration "phobos" { 13 targetName "bench_jwtlited_phobos" 14 dependency "jwtlited:phobos" path="../" 15 versions "USE_PHOBOS" 16 } 17 18 configuration "gnutls" { 19 targetName "bench_jwtlited_gnutls" 20 dependency "jwtlited:gnutls" path="../" 21 versions "USE_GNUTLS" 22 } 23 +/ 24 25 module bench.jwtlited; 26 import core.memory; 27 import std.array; 28 import std.conv; 29 import std.datetime.stopwatch; 30 import std.stdio; 31 version (USE_OPENSSL) import jwtlited.openssl; 32 else version (USE_PHOBOS) import jwtlited.phobos; 33 else version (USE_GNUTLS) import jwtlited.gnutls; 34 35 int main(string[] args) 36 { 37 // args: enc/dec/val, cycle count, alg, token/payload, signature 38 // output: 39 // payload/token/true 40 // msecs taken 41 // GC used bytes 42 43 if (args.length != 6) { writeln("Invalid args"); return 1; } 44 size_t cycles = args[2].to!size_t; 45 46 JWTAlgorithm alg = args[3].to!JWTAlgorithm; 47 48 { 49 StopWatch sw; 50 sw.start(); 51 immutable prevAllocated = GC.allocatedInCurrentThread; 52 scope (exit) 53 { 54 sw.stop(); 55 writeln(sw.peek.total!"msecs"); 56 writeln(GC.allocatedInCurrentThread - prevAllocated); 57 } 58 59 switch (alg) 60 { 61 case JWTAlgorithm.HS256: 62 HS256Handler h; 63 if (!h.loadKey(args[5])) { writeln("Problem loading secret"); return 1; } 64 if (args[1] == "val") return h.validate(cycles, args[4]); 65 else if (args[1] == "dec") return h.decode(cycles, args[4]); 66 else if (args[1] == "enc") return h.encode(cycles, args[4]); 67 break; 68 case JWTAlgorithm.RS256: 69 version (USE_PHOBOS) {} 70 else 71 { 72 RS256Handler h; 73 if (args[1] == "enc") { 74 if (!h.loadPKey(args[5])) { writeln("Problem loading secret"); return 1; } 75 } else { 76 if (!h.loadKey(args[5])) { writeln("Problem loading secret"); return 1; } 77 } 78 if (args[1] == "val") return h.validate(cycles, args[4]); 79 else if (args[1] == "dec") return h.decode(cycles, args[4]); 80 else if (args[1] == "enc") return h.encode(cycles, args[4]); 81 break; 82 } 83 case JWTAlgorithm.ES256: 84 version (USE_PHOBOS) {} 85 else 86 { 87 ES256Handler h; 88 if (args[1] == "enc") { 89 if (!h.loadPKey(args[5])) { writeln("Problem loading secret"); return 1; } 90 } else { 91 if (!h.loadKey(args[5])) { writeln("Problem loading secret"); return 1; } 92 } 93 if (args[1] == "val") return h.validate(cycles, args[4]); 94 else if (args[1] == "dec") return h.decode(cycles, args[4]); 95 else if (args[1] == "enc") return h.encode(cycles, args[4]); 96 break; 97 } 98 default: writeln("Unsupported algorithm"); return 1; 99 } 100 } 101 102 writeln("Invalid command: ", args[1]); 103 return 1; 104 } 105 106 int validate(Handler)(ref Handler h, size_t cycles, string token) 107 { 108 bool res; 109 foreach (_; 0..cycles) 110 res = jwtlited.validate(h, token); 111 writeln(res); 112 return 0; 113 } 114 115 int decode(Handler)(ref Handler h, size_t cycles, string token) 116 { 117 ubyte[512] res; 118 foreach (_; 0..cycles) 119 { 120 if (!jwtlited.decode(h, token, res[])) { 121 writeln("Failed to decode token"); 122 return 1; 123 } 124 } 125 import core.stdc.string; 126 writeln(cast(char[])res[0..strlen(cast(const(char)*)res.ptr)]); 127 return 0; 128 } 129 130 int encode(Handler)(ref Handler h, size_t cycles, string pay) 131 { 132 char[512] buf; 133 int len; 134 foreach (_; 0..cycles) 135 len = jwtlited.encode(h, buf[], pay); 136 writeln(buf[0..len]); 137 return 0; 138 }