Skip to content

Instantly share code, notes, and snippets.

@vieirin
Created July 16, 2019 21:17
Show Gist options
  • Save vieirin/b92af75dac2cf57db8827157ff650fd4 to your computer and use it in GitHub Desktop.
Save vieirin/b92af75dac2cf57db8827157ff650fd4 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"github.com/miekg/pkcs11"
)
func main() {
p := pkcs11.New("/usr/lib/libsofthsm2.so")
err := p.Initialize()
if err != nil {
panic(err)
}
defer p.Destroy()
defer p.Finalize()
slots, err := p.GetSlotList(true)
if err != nil {
panic(err)
}
session, err := p.OpenSession(slots[0], pkcs11.CKF_SERIAL_SESSION|pkcs11.CKF_RW_SESSION)
if err != nil {
panic(err)
}
defer p.CloseSession(session)
err = p.Login(session, pkcs11.CKU_USER, "4321")
if err != nil {
panic(err)
}
defer p.Logout(session)
p.DigestInit(session, []*pkcs11.Mechanism{pkcs11.NewMechanism(pkcs11.CKM_SHA_1, nil)})
hash, err := p.Digest(session, []byte("this is a string"))
if err != nil {
panic(err)
}
for _, d := range hash {
fmt.Printf("%x", d)
}
fmt.Println()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment