dpkcs11 1.0.0
dpkcs11: ^1.0.0 copied to clipboard
PKCS#11 FFI Wrapper for Dart
import 'dart:convert';
import 'package:dpkcs11/dpkcs11.dart';
import 'package:dpkcs11/src/digester_type.dart';
void main(List<String> arguments) {
// String libraryPath = 'C:\\Windows\\System32\\akisp11.dll';
String libraryPath = 'C:\\SoftHSM2\\lib\\softhsm2-x64.dll';
Dpkcs11 dpkcs11 = Dpkcs11(libraryPath: libraryPath);
dpkcs11.digester = DigesterType.hardware;
// Utils.logLevel = Level.debug;
try {
PKCS11Session session = dpkcs11.init().selectFirstSlot().openSession();
try {
session.login("123456");
try {
List<int> hash = session.digest(DigestMethod.SHA_1, "welcome1");
print("Digest: $hash");
PKCS11Keypair? kp = session.getKeyPairByAttribute(
PKCS11Attribute.SIGN,
true,
);
if (kp == null) {
throw Exception("No Key Found for Signing");
}
String datatosign = "Hello World!";
List<int> signature = kp.sign(datatosign);
print("Signature value: ${base64.encode(signature)}");
bool signatureResult = kp.verify(datatosign, signature);
print("Verify signature: $signatureResult");
} finally {
session.logout();
}
} finally {
session.close();
}
} catch (e) {
print("Error: $e");
} finally {
dpkcs11.close();
}
print("bye bye");
}