Created
December 14, 2017 17:00
-
-
Save mechazod/89ea3a1f3ab9b0fbce68ff77ea248349 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment