From 5f911c5be004b1f06cca4b0e0505d90df0850e45 Mon Sep 17 00:00:00 2001 From: Jeremy Woertink Date: Mon, 25 Mar 2024 14:22:28 -0700 Subject: [PATCH] Adds the ability to configure the secret key size generated. Fixes #1847 (#1856) --- spec/tasks/gen/secret_key_base_spec.cr | 14 +++++++++++--- tasks/gen/secret_key.cr | 6 ++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/spec/tasks/gen/secret_key_base_spec.cr b/spec/tasks/gen/secret_key_base_spec.cr index d33ade021..0bd7283e2 100644 --- a/spec/tasks/gen/secret_key_base_spec.cr +++ b/spec/tasks/gen/secret_key_base_spec.cr @@ -2,10 +2,18 @@ require "../../spec_helper" describe Gen::SecretKey do it "outputs a new secret key base" do - io = IO::Memory.new + task = Gen::SecretKey.new + task.output = IO::Memory.new + task.print_help_or_call(args: [] of String) - Gen::SecretKey.new.call(io) + (task.output.to_s.size >= 32).should be_true + end + + it "outputs a larger size when configured" do + task = Gen::SecretKey.new + task.output = IO::Memory.new + task.print_help_or_call(args: ["-n 64"]) - (io.to_s.size >= 32).should be_true + (task.output.to_s.size >= 64).should be_true end end diff --git a/tasks/gen/secret_key.cr b/tasks/gen/secret_key.cr index bddfeb709..fbceafca9 100644 --- a/tasks/gen/secret_key.cr +++ b/tasks/gen/secret_key.cr @@ -3,7 +3,9 @@ require "lucky_task" class Gen::SecretKey < LuckyTask::Task summary "Generate a new secret key" - def call(io : IO = STDOUT) - io.puts Random::Secure.base64(32) + int32 :number, "n random bytes used to encode into base64.", shortcut: "-n", default: 32 + + def call + output.puts Random::Secure.base64(number) end end