# CSPRNG functions

**URL:** https://discourse.julialang.org/t/csprng-functions/3977
**Category:** Statistics
**Created:** [May 29, 2017, 2:04pm UTC](https://discourse.julialang.org/t/csprng-functions/3977 "2017-05-29T14:04:45Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![acad2](https://avatars.discourse-cdn.com/v4/letter/a/f9ae1b/32.png) [@acad2](https://discourse.julialang.org/u/acad2)
#### Post date: [May 29, 2017, 2:04pm UTC](https://discourse.julialang.org/t/csprng-functions/3977/1 "2017-05-29T14:04:45Z")

</div>

Generally in cryptography, we need to use [**Cryptographically Secure Pseudo-Random Number Generator**](https://en.wikipedia.org/wiki/Cryptographically_secure_pseudorandom_number_generator). But I could not find anything like that in Julia.

Python has [CSPRNG](https://docs.python.org/2/library/random.html#random.SystemRandom) which I could use (based on [this](https://stackoverflow.com/a/18047371/2338725) example):

```julia
julia> using PyCall

julia> @pyimport sys

julia> @pyimport random

julia> r = random.SystemRandom()
PyObject <random.SystemRandom object at 0x0000000013302DA8>

julia> r[:randint]
PyObject <bound method Random.randint of <random.SystemRandom object at 0x0000000013302DA8>>

julia> r[:randint](0,sys.maxsize) ### Python3 uses `maxsize`
8681753530439117037

julia> r[:randint](0,sys.maxsize)
5536205657554653966

```

This works pretty well, though, I think it would be cool to have our own CSPRNG functions. Anyone working on this?

---

<div class="post-metadata">

### Author: ![pint](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pint/32/125_2.png) [@pint](https://discourse.julialang.org/u/pint)
#### Post date: [May 29, 2017, 4:10pm UTC](https://discourse.julialang.org/t/csprng-functions/3977/2 "2017-05-29T16:10:25Z")

</div>

there is the RandomDevice type, which is a wrapper for the OS random device.

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [May 29, 2017, 9:10pm UTC](https://discourse.julialang.org/t/csprng-functions/3977/3 "2017-05-29T21:10:34Z")

</div>

Does [https://github.com/faf0/AES.jl](https://github.com/faf0/AES.jl) help at all?

---

<div class="post-metadata">

### Author: ![acad2](https://avatars.discourse-cdn.com/v4/letter/a/f9ae1b/32.png) [@acad2](https://discourse.julialang.org/u/acad2)
#### Post date: [May 30, 2017, 2:26am UTC](https://discourse.julialang.org/t/csprng-functions/3977/4 "2017-05-30T02:26:41Z")

</div>

> [@dpsanders](#):
>
> Does [GitHub - faf0/AES.jl: AES implementation in Julia. Supports 128/192/256-bit keys and several cipher modes of operation.](https://github.com/faf0/AES.jl) help at all?

No! It does not.

1. CSPRNGs are used, for example, to generate the _key_s (but you can [see] ([https://github.com/faf0/AES.jl/blob/master/test/runtests.jl](https://github.com/faf0/AES.jl/blob/master/test/runtests.jl)) only hardcoded _key_s are used). This module has not random component whatsoever.
2. This module is dangerously outdated (last commit was 2 years ago). I received multiple errors while trying. For example, `Uint8` is no longer a data type (replaced by `UInt8`)…

---

<div class="post-metadata">

### Author: ![acad2](https://avatars.discourse-cdn.com/v4/letter/a/f9ae1b/32.png) [@acad2](https://discourse.julialang.org/u/acad2)
#### Post date: [May 30, 2017, 2:45am UTC](https://discourse.julialang.org/t/csprng-functions/3977/5 "2017-05-30T02:45:33Z")

</div>

> [@pint](#):
>
> `RandomDevice`

If the entropy is generated from the OS, then it should be fine. But you have to mentioned it clearly (whether it’s a CSPRNG, is there any caveat…).  
For example, [this](https://docs.python.org/2/library/os.html#os.urandom) documentation specifies:

> … should be unpredictable enough for cryptographic applications, though …

I was reading the documentation for [`RandomDevice`](https://docs.julialang.org/en/release-0.4/stdlib/numbers/), it says:

> [@](#):
>
> there is the `RandomDevice` type, which is a wrapper for the OS random device.

– which is vague.

---

<div class="post-metadata">

### Author: ![pint](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pint/32/125_2.png) [@pint](https://discourse.julialang.org/u/pint)
#### Post date: [May 30, 2017, 7:15am UTC](https://discourse.julialang.org/t/csprng-functions/3977/6 "2017-05-30T07:15:18Z")

</div>

it is exactly the same, i suppose, as python’s system random. on linux, it will be /dev/urandom

---

<div class="post-metadata">

### Author: ![acad2](https://avatars.discourse-cdn.com/v4/letter/a/f9ae1b/32.png) [@acad2](https://discourse.julialang.org/u/acad2)
#### Post date: [June 9, 2017, 9:21am UTC](https://discourse.julialang.org/t/csprng-functions/3977/7 "2017-06-09T09:21:24Z")

</div>

Awesome!

It’ll be nice to incorporate this information in `RandomDevice`’s documentation. And also, is there any [caveat](https://stackoverflow.com/a/20460178/2338725)?
