Skip to content

Instantly share code, notes, and snippets.

@jvehent
Created June 11, 2018 17:07
Show Gist options
  • Save jvehent/69e9e257501f309aa1ddde178aaf756b to your computer and use it in GitHub Desktop.
Save jvehent/69e9e257501f309aa1ddde178aaf756b to your computer and use it in GitHub Desktop.
RSA PKCSA1v15 using CloudHSM and the Crypto11 package
// This code requires a configuration file to initialize the crypto11
// library. Use the following config in crypto11.config:
// {
// "Path" : "/opt/cloudhsm/lib/libcloudhsm_pkcs11.so",
// "TokenLabel": "cavium",
// "Pin" : "$CRYPTO_USER:$PASSWORD"
// }
// then invoke the program with:
// !CKNFAST_DEBUG=2 CRYPTO11_CONFIG_PATH=crypto11.config go run crypto11_sign.go
package main
import (
"crypto"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"fmt"
"log"
"github.com/ThalesIgnite/crypto11"
)
func main() {
key, err := crypto11.FindKeyPair(nil, []byte("rsa2048"))
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", key)
md := sha256.New()
md.Write([]byte("this is input data to be signed"))
sig, err := key.(crypto.Signer).Sign(rand.Reader, md.Sum(nil), crypto.SHA256)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", base64.StdEncoding.EncodeToString(sig))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment