// sign data with secret key sk // return detached sig // this uses crypto_sign instead pf crypto_sign_detached func CryptoSignFucky(msg, sk []byte) []byte { msgbuff := NewBuffer(msg) defer msgbuff.Free() skbuff := NewBuffer(sk) defer skbuff.Free() if skbuff.size != C.crypto_sign_bytes() { log.Println("nacl.CryptoSign() invalid secret key size", len(sk)) return nil } // allocate the signed message buffer sig := malloc(C.crypto_sign_bytes() + msgbuff.size) defer sig.Free() // compute signature siglen := C.ulonglong(0) res := C.crypto_sign(sig.uchar(), &siglen, msgbuff.uchar(), C.ulonglong(msgbuff.size), skbuff.uchar()) if res == 0 { // return copy of signature inside the signed message offset := int(C.crypto_sign_bytes()) return sig.Bytes()[:offset] } // failure to sign log.Println("nacl.CryptoSign() failed") return nil }
// sign data detached with secret key sk func CryptoSignDetached(msg, sk []byte) []byte { msgbuff := NewBuffer(msg) defer msgbuff.Free() skbuff := NewBuffer(sk) defer skbuff.Free() if skbuff.size != C.crypto_sign_bytes() { return nil } // allocate the signature buffer sig := malloc(C.crypto_sign_bytes()) defer sig.Free() // compute signature siglen := C.ulonglong(0) res := C.crypto_sign_detached(sig.uchar(), &siglen, msgbuff.uchar(), C.ulonglong(msgbuff.size), skbuff.uchar()) if res == 0 && siglen == C.ulonglong(C.crypto_sign_bytes()) { // return copy of signature buffer return sig.Bytes() } // failure to sign return nil }
// 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 }
func SignBytes() int { return int(C.crypto_sign_bytes()) }