CARVIEW |
Select Language
HTTP/2 200
date: Wed, 30 Jul 2025 11:08:05 GMT
content-type: text/html; charset=utf-8
vary: X-PJAX, X-PJAX-Container, Turbo-Visit, Turbo-Frame, X-Requested-With,Accept-Encoding, Accept, X-Requested-With
etag: W/"b21759c2f5f0081d33a93334ef88c43b"
cache-control: max-age=0, private, must-revalidate
strict-transport-security: max-age=31536000; includeSubdomains; preload
x-frame-options: deny
x-content-type-options: nosniff
x-xss-protection: 0
referrer-policy: origin-when-cross-origin, strict-origin-when-cross-origin
content-security-policy: default-src 'none'; base-uri 'self'; child-src github.githubassets.com github.com/assets-cdn/worker/ github.com/assets/ gist.github.com/assets-cdn/worker/; connect-src 'self' uploads.github.com www.githubstatus.com collector.github.com raw.githubusercontent.com api.github.com github-cloud.s3.amazonaws.com github-production-repository-file-5c1aeb.s3.amazonaws.com github-production-upload-manifest-file-7fdce7.s3.amazonaws.com github-production-user-asset-6210df.s3.amazonaws.com *.rel.tunnels.api.visualstudio.com wss://*.rel.tunnels.api.visualstudio.com objects-origin.githubusercontent.com copilot-proxy.githubusercontent.com proxy.individual.githubcopilot.com proxy.business.githubcopilot.com proxy.enterprise.githubcopilot.com *.actions.githubusercontent.com wss://*.actions.githubusercontent.com productionresultssa0.blob.core.windows.net/ productionresultssa1.blob.core.windows.net/ productionresultssa2.blob.core.windows.net/ productionresultssa3.blob.core.windows.net/ productionresultssa4.blob.core.windows.net/ productionresultssa5.blob.core.windows.net/ productionresultssa6.blob.core.windows.net/ productionresultssa7.blob.core.windows.net/ productionresultssa8.blob.core.windows.net/ productionresultssa9.blob.core.windows.net/ productionresultssa10.blob.core.windows.net/ productionresultssa11.blob.core.windows.net/ productionresultssa12.blob.core.windows.net/ productionresultssa13.blob.core.windows.net/ productionresultssa14.blob.core.windows.net/ productionresultssa15.blob.core.windows.net/ productionresultssa16.blob.core.windows.net/ productionresultssa17.blob.core.windows.net/ productionresultssa18.blob.core.windows.net/ productionresultssa19.blob.core.windows.net/ github-production-repository-image-32fea6.s3.amazonaws.com github-production-release-asset-2e65be.s3.amazonaws.com insights.github.com wss://alive.github.com api.githubcopilot.com api.individual.githubcopilot.com api.business.githubcopilot.com api.enterprise.githubcopilot.com; font-src github.githubassets.com; form-action 'self' github.com gist.github.com copilot-workspace.githubnext.com objects-origin.githubusercontent.com; frame-ancestors 'none'; frame-src viewscreen.githubusercontent.com notebooks.githubusercontent.com; img-src 'self' data: blob: github.githubassets.com media.githubusercontent.com camo.githubusercontent.com identicons.github.com avatars.githubusercontent.com private-avatars.githubusercontent.com github-cloud.s3.amazonaws.com objects.githubusercontent.com release-assets.githubusercontent.com secured-user-images.githubusercontent.com/ user-images.githubusercontent.com/ private-user-images.githubusercontent.com opengraph.githubassets.com copilotprodattachments.blob.core.windows.net/github-production-copilot-attachments/ github-production-user-asset-6210df.s3.amazonaws.com customer-stories-feed.github.com spotlights-feed.github.com objects-origin.githubusercontent.com *.githubusercontent.com; manifest-src 'self'; media-src github.com user-images.githubusercontent.com/ secured-user-images.githubusercontent.com/ private-user-images.githubusercontent.com github-production-user-asset-6210df.s3.amazonaws.com gist.github.com; script-src github.githubassets.com; style-src 'unsafe-inline' github.githubassets.com; upgrade-insecure-requests; worker-src github.githubassets.com github.com/assets-cdn/worker/ github.com/assets/ gist.github.com/assets-cdn/worker/
server: github.com
content-encoding: gzip
accept-ranges: bytes
set-cookie: _gh_sess=SdzUdnTW2ga6of%2Fgzvv85%2BuoYcensNUdZScHUMqZJUgesyDdqZFYy%2FifpkbJVNReH8Pl%2Bc0zJd5wxmZwhMIG5DItqWnMxSp3QGULDb8XoHDGZ%2BCBe8tfy3yO33MHbFSKGe0%2B%2B%2FZcxF8zTAQuD5sVdzaopVXguNFi6%2F5glpbpII9BRoXMh6PnwuxKnxsTuGoxJpZ0%2FsxVadU8Iw6KF2TcHfyUCjnhSKKg3N%2BrEVkqLw52TKmMNS2l7z%2Bz1V7eIMMVjbbrqcJWVWyTeRcoEYNPvQ%3D%3D--1ASDRBo8cfYwod6D--tNZ9RtMK04IAYqa%2BcjGrIg%3D%3D; Path=/; HttpOnly; Secure; SameSite=Lax
set-cookie: _octo=GH1.1.725089845.1753873685; Path=/; Domain=github.com; Expires=Thu, 30 Jul 2026 11:08:05 GMT; Secure; SameSite=Lax
set-cookie: logged_in=no; Path=/; Domain=github.com; Expires=Thu, 30 Jul 2026 11:08:05 GMT; HttpOnly; Secure; SameSite=Lax
x-github-request-id: 9F4A:32AE6E:90F0FA:AE3BC7:6889FD14
Elixir ruby-like SecureRandom · GitHub
Show Gist options
Save Myuzu/7367461 to your computer and use it in GitHub Desktop.
{{ message }}
Instantly share code, notes, and snippets.
Last active
August 7, 2022 20:09
-
Star
22
(22)
You must be signed in to star a gist -
Fork
7
(7)
You must be signed in to fork a gist
-
Save Myuzu/7367461 to your computer and use it in GitHub Desktop.
Elixir ruby-like SecureRandom
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# UPD from 2018: | |
# This gist was written for pre-1.0 version of Elixir and won't work on post-1.0 versions. | |
# You probably consider using something else! | |
defmodule SecureRandom do | |
@moduledoc """ | |
Ruby-like SecureRandom module. | |
## Examples | |
iex> SecureRandom.base64 | |
"xhTcitKZI8YiLGzUNLD+HQ==" | |
iex> SecureRandom.urlsafe_base64(4) | |
"pLSVJw" | |
""" | |
@default_length 16 | |
@doc """ | |
Returns random Base64 encoded string. | |
## Examples | |
iex> SecureRandom.base64 | |
"rm/JfqH8Y+Jd7m5SHTHJoA==" | |
iex> SecureRandom.base64(8) | |
"2yDtUyQ5Xws=" | |
""" | |
def base64(n // @default_length) when is_integer n do | |
random_bytes(n) | |
|> :base64.encode_to_string | |
|> to_string | |
end | |
@doc """ | |
Returns random urlsafe Base64 encoded string. | |
## Examples | |
iex> SecureRandom.urlsafe_base64 | |
"xYQcVfWuq6THMY_ZVmG0mA" | |
iex> SecureRandom.urlsafe_base64(8) | |
"8cN__l-6wNw" | |
""" | |
def urlsafe_base64(n // @default_length) when is_integer n do | |
random_bytes(n) | |
|> :base64.encode_to_string | |
|> to_string | |
|> String.replace(%r/[\n\=]/, "") | |
|> String.replace(%r/\+/, "-") | |
|> String.replace(%r/\//, "_") | |
end | |
@doc """ | |
Returns random hex string. | |
## Examples | |
iex> SecureRandom.hex | |
"c3d3b6cdab81a7382fbbae33407b3272" | |
iex> SecureRandom.hex(8) | |
"125583e32b698259" | |
""" | |
def hex(n // @default_length) when is_integer n do | |
random_bytes(n) | |
|> bitstring_to_list | |
|> Enum.map(fn (x) -> integer_to_binary(x, 16) end) | |
|> Enum.join | |
|> String.downcase | |
end | |
@doc """ | |
Returns UUID v4. Not implemented yet. | |
""" | |
def uuid do | |
raise NotImplemented | |
end | |
@doc """ | |
Returns random number. | |
## Examples | |
iex> SecureRandom.random_number | |
0.11301116455519611 | |
iex> SecureRandom.random_number(42) | |
31 | |
iex> SecureRandom.random_number(14, 42) | |
18 | |
iex> SecureRandom.random_number(14..42) | |
28 | |
""" | |
def random_number do | |
:random.seed({:crypto.rand_uniform(1, 99999), :crypto.rand_uniform(1, 99999), :crypto.rand_uniform(1, 99999)}) | |
:random.uniform | |
end | |
def random_number(x) when is_integer x do | |
:crypto.rand_uniform(1, x) | |
end | |
def random_number(range) when is_range range do | |
:crypto.rand_uniform(Range.first(range), Range.last(range)) | |
end | |
def random_number(x, y) when is_integer(x) and is_integer(y) do | |
:crypto.rand_uniform(x, y) | |
end | |
@doc """ | |
Returns random bytes. | |
## Examples | |
iex> SecureRandom.random_bytes | |
<<202, 104, 227, 197, 25, 7, 132, 73, 92, 186, 242, 13, 170, 115, 135, 7>> | |
iex> SecureRandom.random_bytes(8) | |
<<231, 123, 252, 174, 156, 112, 15, 29>> | |
""" | |
def random_bytes(n // @default_length) when is_integer n do | |
:crypto.strong_rand_bytes(n) | |
end | |
end | |
Also, the regular expressions in urlsafe_base64
won't even make this compile. Correct version is:
def urlsafe_base64(n // @default_length) when is_integer n do
random_bytes(n)
|> :base64.encode_to_string
|> to_string
|> String.replace(~r/[\n\=]/, "")
|> String.replace(~r/\+/, "-")
|> String.replace(~r/\//, "_")
end
All in all, I discourage all future visitors from using this gist.
@lucidstack and @robconery, wow, I just saw this comments during my gists "maintenance". Guys it was naive implementation wrote for pre-1.0 version of Elixir. Since then base syntax of the language changed, so it definitely won't work on post-1.0 versions of Elixir.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can’t perform that action at this time.
Default args use
\\
- otherwise you'll have a compilation error.