Skip to content

Instantly share code, notes, and snippets.

@potatosalad
potatosalad / jose-example.exs
Last active January 23, 2017 22:50
RSA jwk-set+json
secret_jwk = JOSE.JWK.generate_key({:rsa, 2048})
# Save the secret RSA key to a PEM file.
JOSE.JWK.to_pem_file("rsa-secret.pem", secret_jwk)
# Or optionally as a password protected PEM file.
JOSE.JWK.to_pem_file("secret-password", "rsa-secret-protected.pem", secret_jwk)
# Save the jwk-set+json to a file.
public_jwk = secret_jwk |> JOSE.JWK.to_public()
JOSE.JWK.to_file("public-rsa.json", :jose_jwk_set.to_map([public_jwk |> JOSE.JWK.to_record()], %{}))
# Or use JOSE.JWK.to_public_file/2 to skip converting secret_jwk to public_jwk
JOSE.JWK.to_public_file("public-rsa.json", :jose_jwk_set.to_map([secret_jwk |> JOSE.JWK.to_record()], %{}))
@potatosalad
potatosalad / sodium-message_encryptor.ex
Last active October 14, 2016 16:44
Example plug message encryptor using libsodium
defmodule Sodium.MessageEncryptor do
@moduledoc ~S"""
AES256-GCM Content-Encryption with AES256-GCM Key-Wrapping using [libsodium](https://github.com/potatosalad/erlang-libsodium).
For example:
# Encryption: `secret' must be at least 256-bits, `sign_secret' may be any length
iex> encrypted = Sodium.MessageEncryptor.encrypt("test", "abcdefghijklmnopqrstuvwxyz012345", "abcdefghijklmnop")
"QTI1NkdDTQ.VGbRU_MS0G_sFvfpUkoXmK8Hl1TBI5tPVN_SUl6NI7yYrZDgdV3u2OrnWwXAgg8A-FZNOtJV7P-nICoQ.Ht5ty7EvPx-YY-bn.98CpxA.1KZaBS79yPKdXuTbdqL66A"
@potatosalad
potatosalad / libsodium_aes_gcm_example.exs
Created October 13, 2016 21:33
Example usages of AES-GCM with libsodium
# Static Constants
plain_text = "test"
plain_text_size = byte_size(plain_text)
aad = "A256GCM" # AAD (Additional Authenticated Data)
####################################
# Example 1: Direct Key Encryption #
####################################
# In this example, the CEK must be known by the encryptor and decryptor (ie. a shared secret)
@potatosalad
potatosalad / nifsy-dirty.md
Created July 28, 2016 15:56
nifsy dirty schedulers vs uv (preliminary benchmark)
name total usec/iter usec/line
1 KB (16 B) - nifsy 6728 134.56 2.990
1 KB (1 KB) - nifsy 5176 103.52 2.300
1 KB (64KB) - nifsy 5329 106.58 2.368
1 MB (1 KB) - nifsy 210460 4209.20 2.907
1 MB (64KB) - nifsy 169180 3383.60 2.337
1 MB (1 MB) - nifsy 194220 3884.40 2.683

Keybase proof

I hereby claim:

  • I am potatosalad on github.
  • I am potatosalad (https://keybase.io/potatosalad) on keybase.
  • I have a public key whose fingerprint is DE0E 3767 CFFB D7F4 D978 1FB9 E2D9 1EA8 2EDA 44FF

To claim this, I am signing this object:

@potatosalad
potatosalad / jwk_ec256_alice_sk.json
Last active January 19, 2016 21:38
Example keys for JOSE.JWE documentation https://hexdocs.pm/jose
{
"crv": "P-256",
"d": "JPUl6o2Or-ez1P6xErxJvgvDcqz-gIOCdTmc14Kw0Dw",
"kty": "EC",
"x": "48QU3Q0CySxwJbEwWrJrYXlp88_dVpHTxq4avc66h5Q",
"y": "ZzqrINtMMxHxQ-QB72RMcdlmDsOIwlKhMqVm_gYWC14"
}
# variables used in examples below
p = 2^448 - 2^224 - 1
u = 5
v = 355293926785568175264127502063783334808976399387714271831880898435169088786967410002932673765864550910142774147268105838985595290606362
x = 224580040295924300187604334099896036246789641632564134246125461686950415467406032909029192869357953282578032075146446173674602635247710
y = 298819210078481492676017930443930673437544040154080242095928241372331506189835876003536878655418784733982303233503462500531545062832660
# testing (u, v) functions; both return True
u == y^2/x^2 % p
v == (2 - x^2 - y^2)*y/x^3 % p
# Map using maps used by examples:
map_of_maps = %{
%{key1: :a, key2: :b} => %{val1: 1, val2: 2},
%{key1: :a, key2: :c} => %{val1: 3, val2: 8},
%{key1: :b, key2: :d} => %{val1: 5, val2: 3}
}
# Map using tuples used by examples:
map_of_tuples = %{
{:a, :b} => {1, 2},
@potatosalad
potatosalad / rsa-decrypted.pem
Created November 30, 2015 17:09
Example decrypted and encrypted RSA 512-bit key (password is "secret")
-----BEGIN RSA PRIVATE KEY-----
MIIBPQIBAAJBANkpF/lVw/COC3QGJmWfw5Kay97N7+O2LaKdo+eWuEFnCxGEiB67
Q7/q7ntJeDg+vxMXK30w8w18e2Xjd9AqFY8CAwEAAQJBAII9RBSUnGwENNhD6/lc
5QVi80xQBER0eVAi7S8y1JTb5Sqc/NPFT9KqeI8RpEv+uRx5K24hFzkkAFxRH2Eg
gtkCIQD/hxD/7VrrRmWqAHicbmm9yvMktT53kCKNPspVbnUNewIhANmP3ocFpw4g
hdN/tLgjQE1eCUIMhVvGicx9mK1WpFn9AiEAt0SMMMJv3ybSa6eC2c9nD7Srxrwn
XWGHJn7OZ4+dbFkCIQCQeB+9q3w+Pkx/oHriF854Uax2Jphb1B/nIbPDzxf+BQIh
AI5O3sI8LYKvBJhBTnzzWfjYQMaRmHMyz+0ZBbgfwXPq
-----END RSA PRIVATE KEY-----
@potatosalad
potatosalad / jwk_ed25519.json
Last active June 28, 2019 10:14
Example keys for JOSE.JWS documentation https://hexdocs.pm/jose
{
"crv": "Ed25519",
"d": "VoU6Pm8SOjz8ummuRPsvoJQOPI3cjsdMfUhf2AAEc7s",
"kty": "OKP",
"x": "l11mBSuP-XxI0KoSG7YEWRp4GWm7dKMOPkItJy2tlMM"
}