crypto_box_seal.go 957 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package cryptobox
  2. // #cgo pkg-config: libsodium
  3. // #include <stdlib.h>
  4. // #include <sodium.h>
  5. import "C"
  6. import support "kkscrap-go/locals/gosodium/support"
  7. func CryptoBoxSeal(m []byte, pk []byte) ([]byte, int) {
  8. support.CheckSize(pk, CryptoBoxPublicKeyBytes(), "public key")
  9. c := make([]byte, len(m)+CryptoBoxSealBytes())
  10. exit := int(C.crypto_box_seal(
  11. (*C.uchar)(&c[0]),
  12. (*C.uchar)(&m[0]),
  13. (C.ulonglong)(len(m)),
  14. (*C.uchar)(&pk[0])))
  15. return c, exit
  16. }
  17. func CryptoBoxSealOpen(c []byte, pk []byte, sk []byte) ([]byte, int) {
  18. support.CheckSize(pk, CryptoBoxPublicKeyBytes(), "public key")
  19. support.CheckSize(sk, CryptoBoxSecretKeyBytes(), "secret key")
  20. m := make([]byte, len(c)-CryptoBoxSealBytes())
  21. exit := int(C.crypto_box_seal_open(
  22. (*C.uchar)(&m[0]),
  23. (*C.uchar)(&c[0]),
  24. (C.ulonglong)(len(c)),
  25. (*C.uchar)(&pk[0]),
  26. (*C.uchar)(&sk[0])))
  27. return m, exit
  28. }
  29. func CryptoBoxSealBytes() int {
  30. return int(C.crypto_box_sealbytes())
  31. }