 (defn randombytes
   "Create a byte array with n random bytes."
   [n]
-  (let [buf (byte-array n)]
-    (random-to-byte-array! buf n)))
+  (bb/->bytes (random-to-buf! (bb/alloc n) n)))

            [caesium.byte-bufs :as bb])
  (:import (java.nio ByteBuffer)))

(defn random-to-byte-array!
  "**DANGER** This function is low-level, you only want to use it if you are
  managing your own buffers. See [[randombytes]] for a high level API that
  creates the byte array buffer for you.

  Given a byte array, populate it with n random bytes.

  If n is not given, populates the entire array."
  ([^bytes arr]
   (random-to-byte-array! arr (bb/buflen arr)))
  ([^bytes arr n]
   (.randombytes b/sodium arr (long n))
   arr))

(defn random-to-byte-buffer!
  "**DANGER** This function is low-level, you only want to use it if you are
  managing your own buffers. See [[randombytes]] for a high level API that
  creates the buffer for you.


  If n is not given, populates the entire buffer."
  ([^ByteBuffer buf]
   (random-to-buf! buf (.remaining buf)))
  ([^ByteBuffer buf n]
   (.randombytes b/sodium buf (long n))
   buf))
