Guest User

Untitled

a guest
Aug 17th, 2019
110
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package sodium_generic_hash_benchmark
  2.  
  3. import (
  4. "fmt"
  5. "unsafe"
  6. )
  7.  
  8. // #cgo pkg-config: libsodium
  9. // #include <stdlib.h>
  10. // #include <sodium.h>
  11. import "C"
  12.  
  13. func init() {
  14. var r int
  15. if r = int(C.sodium_init()); 0 != r {
  16. panic(fmt.Errorf("sodium_init(): rc = %d, expect rc = 0", r))
  17. }
  18. }
  19.  
  20. func sodiumRandbytes(b []byte) {
  21. if 0 == len(b) {
  22. panic("target buffer is nil or zero-length")
  23. }
  24.  
  25. var bp = unsafe.Pointer(&b[0])
  26. var bs = C.size_t(len(b))
  27.  
  28. C.randombytes_buf(bp, bs)
  29. }
  30.  
  31. func sodiumGenerichash(p, k, m []byte) int {
  32. var pp = (*C.uchar)(unsafe.Pointer(&p[0]))
  33. var ps = C.size_t(24)
  34. if cap(p) < int(ps) {
  35. panic("target buffer length less than required outlen")
  36. }
  37.  
  38. var kp = (*C.uchar)(unsafe.Pointer(&k[0]))
  39. var ks = C.size_t(24)
  40. if len(k) < int(ks) {
  41. panic("key length less than required")
  42. }
  43.  
  44. var mp = (*C.uchar)(unsafe.Pointer(&m[0]))
  45. var ms = C.ulonglong(len(m))
  46. if 0 == len(m) {
  47. panic("message buffer is nil or zero-length")
  48. }
  49.  
  50. return int(C.crypto_generichash(pp, ps, mp, ms, kp, ks))
  51. }
RAW Paste Data