Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var graphene = require("graphene-pk11");
- var Module = graphene.Module;
- var lib = "/opt/local/lib/softhsm/libsofthsm2.so";
- var mod = Module.load(lib, "SoftHSM");
- var SessionFlag = graphene.SessionFlag;
- var UserType = graphene.UserType;
- mod.initialize();
- var slot = mod.getSlots(2);
- if (slot.flags & graphene.SlotFlag.TOKEN_PRESENT) {
- var session = slot.open(SessionFlag.SERIAL_SESSION | SessionFlag.RW_SESSION);
- session.login("8888",UserType.USER);
- // enc algorithm
- var alg = {
- name: "AES_CBC_PAD",
- params: new Buffer([1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6]) // IV
- };
- // generate AES key
- var key = session.generateKey(graphene.KeyGenMechanism.AES, {
- "class": graphene.ObjectClass.SECRET_KEY,
- "token": false,
- "valueLen": 256 / 8,
- "keyType": graphene.KeyType.AES,
- "encrypt": true,
- "decrypt": true
- });
- var MESSAGE = "helloworld";
- var enc = new Buffer(0);
- // encrypting
- var enc = session.createCipher(alg, key).once(MESSAGE, new Buffer((MESSAGE.length >> 3 << 3) + 16));
- console.log("Enc:", enc);
- console.log("Enc:", enc.toString("hex")); // Enc: eb21e15b896f728a4...
- session.logout();
- session.close();
- }
- else {
- console.error("Slot is not initialized");
- }
- mod.finalize();
Add Comment
Please, Sign In to add comment