Created
April 11, 2015 01:03
-
-
Save diogomonica/89dd1e913c66421df284 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
package main | |
import ( | |
"fmt" | |
"github.com/miekg/pkcs11" | |
) | |
func main() { | |
p := pkcs11.New("/usr/local/lib/softhsm/libsofthsm2.so") | |
p.Initialize() | |
defer p.Destroy() | |
defer p.Finalize() | |
slots, err := p.GetSlotList(true) | |
if err != nil { | |
fmt.Println(err) | |
} | |
session, err := p.OpenSession(slots[0], pkcs11.CKF_SERIAL_SESSION|pkcs11.CKF_RW_SESSION) | |
if err != nil { | |
fmt.Println(err) | |
} | |
defer p.CloseSession(session) | |
p.Login(session, pkcs11.CKU_USER, "1234") | |
defer p.Logout(session) | |
publicKeyTemplate := []*pkcs11.Attribute{ | |
pkcs11.NewAttribute(pkcs11.CKA_KEY_TYPE, pkcs11.CKO_PUBLIC_KEY), | |
pkcs11.NewAttribute(pkcs11.CKA_TOKEN, true), | |
pkcs11.NewAttribute(pkcs11.CKA_ENCRYPT, true), | |
pkcs11.NewAttribute(pkcs11.CKA_PUBLIC_EXPONENT, []byte{3}), | |
pkcs11.NewAttribute(pkcs11.CKA_MODULUS_BITS, 1024), | |
pkcs11.NewAttribute(pkcs11.CKA_LABEL, "MyFirstKey"), | |
} | |
privateKeyTemplate := []*pkcs11.Attribute{ | |
pkcs11.NewAttribute(pkcs11.CKA_KEY_TYPE, pkcs11.CKO_PRIVATE_KEY), | |
pkcs11.NewAttribute(pkcs11.CKA_TOKEN, true), | |
pkcs11.NewAttribute(pkcs11.CKA_PRIVATE, true), | |
pkcs11.NewAttribute(pkcs11.CKA_SIGN, true), | |
pkcs11.NewAttribute(pkcs11.CKA_LABEL, "MyFirstKey"), | |
} | |
pub, priv, err := p.GenerateKeyPair(session, | |
[]*pkcs11.Mechanism{pkcs11.NewMechanism(pkcs11.CKM_RSA_PKCS_KEY_PAIR_GEN, nil)}, | |
publicKeyTemplate, privateKeyTemplate) | |
if err != nil { | |
fmt.Println(err) | |
} | |
p.SignInit(session, []*pkcs11.Mechanism{pkcs11.NewMechanism(pkcs11.CKM_SHA1_RSA_PKCS, nil)}, priv) | |
// Sign something with the private key. | |
data := []byte("Lets sign this data") | |
fmt.Println(string(data)) | |
sig, err := p.Sign(session, data) | |
if err != nil { | |
fmt.Println(err) | |
} else { | |
fmt.Printf("%v validate with %v\n", sig, pub) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment