// generate a keypair func GenSignKeypair() *KeyPair { sk_len := C.crypto_sign_secretkeybytes() sk := malloc(sk_len) pk_len := C.crypto_sign_publickeybytes() pk := malloc(pk_len) res := C.crypto_sign_keypair(pk.uchar(), sk.uchar()) if res == 0 { return &KeyPair{pk, sk} } pk.Free() sk.Free() return nil }
// generate a keypair func GenSignKeypair() *KeyPair { sk_len := C.crypto_sign_secretkeybytes() sk := malloc(sk_len) pk_len := C.crypto_sign_publickeybytes() pk := malloc(pk_len) res := C.crypto_sign_keypair(pk.uchar(), sk.uchar()) if res == 0 { return &KeyPair{pk, sk} } log.Println("nacl.GenSignKeypair() failed to generate keypair") pk.Free() sk.Free() return nil }
// verify a signed message func CryptoVerify(smsg, pk []byte) bool { smsg_buff := NewBuffer(smsg) defer smsg_buff.Free() pk_buff := NewBuffer(pk) defer pk_buff.Free() if pk_buff.size != C.crypto_sign_publickeybytes() { return false } mlen := C.ulonglong(0) msg := malloc(C.size_t(len(smsg))) defer msg.Free() smlen := C.ulonglong(smsg_buff.size) return C.crypto_sign_open(msg.uchar(), &mlen, smsg_buff.uchar(), smlen, pk_buff.uchar()) != -1 }
// verfiy a detached signature // return true on valid otherwise false func CryptoVerifyDetached(msg, sig, pk []byte) bool { msg_buff := NewBuffer(msg) defer msg_buff.Free() sig_buff := NewBuffer(sig) defer sig_buff.Free() pk_buff := NewBuffer(pk) defer pk_buff.Free() if pk_buff.size != C.crypto_sign_publickeybytes() { return false } // invalid sig size if sig_buff.size != C.crypto_sign_bytes() { return false } return C.crypto_sign_verify_detached(sig_buff.uchar(), msg_buff.uchar(), C.ulonglong(len(msg)), pk_buff.uchar()) == 0 }
// make keypair from seed func LoadSignKey(seed []byte) *KeyPair { seed_len := C.crypto_sign_seedbytes() if C.size_t(len(seed)) != seed_len { return nil } seedbuff := NewBuffer(seed) defer seedbuff.Free() pk_len := C.crypto_sign_publickeybytes() sk_len := C.crypto_sign_secretkeybytes() pkbuff := malloc(pk_len) skbuff := malloc(sk_len) res := C.crypto_sign_seed_keypair(pkbuff.uchar(), skbuff.uchar(), seedbuff.uchar()) if res != 0 { pkbuff.Free() skbuff.Free() return nil } return &KeyPair{pkbuff, skbuff} }
// make keypair from seed func LoadSignKey(seed []byte) *KeyPair { seed_len := C.crypto_sign_seedbytes() if C.size_t(len(seed)) != seed_len { log.Println("nacl.SeedSignKey() invalid seed size", len(seed)) return nil } seedbuff := NewBuffer(seed) defer seedbuff.Free() pk_len := C.crypto_sign_publickeybytes() sk_len := C.crypto_sign_secretkeybytes() pkbuff := malloc(pk_len) skbuff := malloc(sk_len) res := C.crypto_sign_seed_keypair(pkbuff.uchar(), skbuff.uchar(), seedbuff.uchar()) if res != 0 { log.Println("nacl.SeedSignKey cannot derive keys from seed", res) pkbuff.Free() skbuff.Free() return nil } return &KeyPair{pkbuff, skbuff} }
// get public key from secret key func GetSignPubkey(sk []byte) ([]byte, error) { sk_len := C.crypto_sign_secretkeybytes() if C.size_t(len(sk)) != sk_len { return nil, errors.New(fmt.Sprintf("nacl.GetSignPubkey() invalid secret key size %d != %d", len(sk), sk_len)) } pk_len := C.crypto_sign_publickeybytes() pkbuff := malloc(pk_len) defer pkbuff.Free() skbuff := NewBuffer(sk) defer skbuff.Free() //XXX: hack res := C.crypto_sign_seed_keypair(pkbuff.uchar(), skbuff.uchar(), skbuff.uchar()) if res != 0 { return nil, errors.New(fmt.Sprintf("nacl.GetSignPubkey() failed to get public key from secret key: %d", res)) } return pkbuff.Bytes(), nil }
func SignPublicKeyBytes() int { return int(C.crypto_sign_publickeybytes()) }
func CryptoSignPublicLen() int { return int(C.crypto_sign_publickeybytes()) }