Skip to content
This repository was archived by the owner on Apr 22, 2025. It is now read-only.

Commit 9c20060

Browse files
committed
chore: Fix urlsafe_base64/1 generation
The test assumes 22 byte default, but had 32 because it was double-base64 encoding (once regular, and once URL-safe). Without the double encoding, the tests fail because they were expecting 32 (then 24), but the intent was URL-safe base encoding without padding. Originally based on patricksrobertson#13, but fixes the tests and padding.
1 parent 291edd5 commit 9c20060

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

lib/secure_random.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ defmodule SecureRandom do
7171
"""
7272
def urlsafe_base64(n \\ @default_length) do
7373
n
74-
|> base64()
75-
|> Base.url_encode64(padding: true)
74+
|> random_bytes()
75+
|> Base.url_encode64(padding: false)
7676
end
7777

7878
@doc """

test/secure_random_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ defmodule SecureRandomTest do
2828
end
2929

3030
test "urlsafe_base64/1 defaults to a string length of 22" do
31-
assert 32 == String.length(SecureRandom.urlsafe_base64())
31+
assert 22 == String.length(SecureRandom.urlsafe_base64())
3232
end
3333

3434
test "urlsafe_base64/1 takes a byte_length" do
35-
assert 12 == String.length(SecureRandom.urlsafe_base64(4))
35+
assert 6 == String.length(SecureRandom.urlsafe_base64(4))
3636
end
3737

3838
test "uuid/0 returns a Binary string" do

0 commit comments

Comments
 (0)