Skip to content

Commit 145cb78

Browse files
authored
Merge pull request #84 from tristanmorgan/cli-digest
Add digest parameter to CLI.
2 parents 397e940 + 8ce2d2a commit 145cb78

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

lib/rotp/arguments.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ def parser
6868
parser.on_tail('-h', '--help', 'Show this message') do
6969
options!.mode = :help
7070
end
71+
72+
parser.on('-d', '--digest [ALGORITHM]', 'Use algorithm for the digest (default sha1)') do |digest|
73+
options!.digest = digest
74+
end
7175
end
7276
end
7377

lib/rotp/cli.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ def output
3131
return arguments.to_s if options.mode == :help
3232

3333
if options.mode == :time
34-
ROTP::TOTP.new(options.secret).now
34+
ROTP::TOTP.new(options.secret, options).now
3535
elsif options.mode == :hmac
36-
ROTP::HOTP.new(options.secret).at options.counter
36+
ROTP::HOTP.new(options.secret, options).at options.counter
3737
end
3838
end
3939

spec/lib/rotp/cli_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@
1818
end
1919
end
2020

21+
context 'generating a TOTP with sha256 digest' do
22+
let(:argv) { %w[--secret JBSWY3DPEHPK3PXP --digest sha256] }
23+
24+
it 'prints the corresponding token' do
25+
expect(output).to eq '544902'
26+
end
27+
end
28+
2129
context 'generating a TOTP with no secret' do
2230
let(:argv) { %w[--time --secret] }
2331

@@ -49,4 +57,12 @@
4957
expect(output).to eq '161024'
5058
end
5159
end
60+
61+
context 'generating a HOTP' do
62+
let(:argv) { %W[--hmac --secret #{'a' * 32} --counter 1234 --digest sha256] }
63+
64+
it 'prints the corresponding token' do
65+
expect(output).to eq '325941'
66+
end
67+
end
5268
end

0 commit comments

Comments
 (0)