mintotp 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.github/workflows/gem-test-push.yml +38 -0
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/Gemfile +9 -0
- data/LICENSE.txt +21 -0
- data/README.md +124 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/mintotp/totp.rb +36 -0
- data/lib/mintotp/version.rb +3 -0
- data/lib/mintotp.rb +5 -0
- data/mintotp.gemspec +31 -0
- metadata +87 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 244de13102782d461fe608e4f77f29532c73da7f76fcbc538f0aaa024e14f3ad
|
4
|
+
data.tar.gz: 1e5eff91ba2f195607fd450540d0886acbb9a81f8ed8326c5a7f373ec46d6216
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 44e395af6da8c360571fb430cd7f43e5212464cfdd852f429526d79622f4f82f75d0a03e4b9bc9b0d35215ebe50c81901e64106cee2b7083b01b70dddeb649db
|
7
|
+
data.tar.gz: 987a4d8afc28bce8471ffac17055ca8b1cc47055e13dd522e7c3e259c57297fc42c63a252680f38e2876d18c5abcc4e0eabe293d57c124253222b1c3472cc370
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# This workflow uses actions that are not certified by GitHub.
|
2
|
+
# They are provided by a third-party and are governed by
|
3
|
+
# separate terms of service, privacy policy, and support
|
4
|
+
# documentation.
|
5
|
+
# This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
|
6
|
+
# For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
|
7
|
+
|
8
|
+
name: Mintotp
|
9
|
+
|
10
|
+
on:
|
11
|
+
push:
|
12
|
+
branches: [ main ]
|
13
|
+
pull_request:
|
14
|
+
branches: [ main ]
|
15
|
+
|
16
|
+
permissions:
|
17
|
+
contents: read
|
18
|
+
|
19
|
+
jobs:
|
20
|
+
test:
|
21
|
+
|
22
|
+
runs-on: ubuntu-latest
|
23
|
+
strategy:
|
24
|
+
matrix:
|
25
|
+
ruby-version: ['2.6', '2.7', '3.0']
|
26
|
+
|
27
|
+
steps:
|
28
|
+
- uses: actions/checkout@v3
|
29
|
+
- name: Set up Ruby
|
30
|
+
# To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
|
31
|
+
# change this to (see https://github.com/ruby/setup-ruby#versioning):
|
32
|
+
# uses: ruby/setup-ruby@v1
|
33
|
+
uses: ruby/setup-ruby@2b019609e2b0f1ea1a2bc8ca11cb82ab46ada124
|
34
|
+
with:
|
35
|
+
ruby-version: ${{ matrix.ruby-version }}
|
36
|
+
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
|
37
|
+
- name: Run tests
|
38
|
+
run: bundle exec rake spec
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 win
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
# Mintotp
|
2
|
+
|
3
|
+
Mintotp is a minimal TOTP generator written in Ruby. Inspired by [this library](https://github.com/susam/mintotp).
|
4
|
+
|
5
|
+
[](https://github.com/winhtaikaung/mintotp-ruby/actions/workflows/gem-test-push.yml)
|
6
|
+
|
7
|
+
[](https://github.com/winhtaikaung/mintotp-ruby/issues)
|
8
|
+
[](https://github.com/winhtaikaung/mintotp-ruby/blob/main/LICENSE.txt)
|
9
|
+
|
10
|
+
|
11
|
+
<!-- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/mintotp`. To experiment with that code, run `bin/console` for an interactive prompt. -->
|
12
|
+
|
13
|
+
<!-- TODO: Delete this and the text above, and describe your gem -->
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Add this line to your application's Gemfile:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
gem 'mintotp'
|
21
|
+
```
|
22
|
+
|
23
|
+
And then execute:
|
24
|
+
|
25
|
+
$ bundle install
|
26
|
+
|
27
|
+
Or install it yourself as:
|
28
|
+
|
29
|
+
$ gem install mintotp
|
30
|
+
|
31
|
+
## Usage
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
SECRET1 = "ZYTYYE5FOAGW5ML7LRWUL4WTZLNJAMZS"
|
35
|
+
minTotp = Mintotp::TOTP.new()
|
36
|
+
minTotp.totp(SECRET1) # => return the same time based token from Google Authenticator
|
37
|
+
```
|
38
|
+
Source Code
|
39
|
+
-----------
|
40
|
+
|
41
|
+
At the heart of the TOTP algorithm lies the HOTP algorithm. HOTP stands
|
42
|
+
for HMAC-based One-Time Password. HMAC stands for Hash-based Message
|
43
|
+
Authentication Code. Here are the relevant RFCs to learn more about
|
44
|
+
these algorithms:
|
45
|
+
|
46
|
+
- [RFC 2104]: HMAC: Keyed-Hashing for Message Authentication
|
47
|
+
- [RFC 4226]: HOTP: An HMAC-Based One-Time Password Algorithm
|
48
|
+
- [RFC 6238]: TOTP: Time-Based One-Time Password Algorithm
|
49
|
+
|
50
|
+
[RFC 2104]: https://tools.ietf.org/html/rfc2104
|
51
|
+
[RFC 4226]: https://tools.ietf.org/html/rfc4226
|
52
|
+
[RFC 6238]: https://tools.ietf.org/html/rfc6238
|
53
|
+
|
54
|
+
The source code in [`totp.rb`](https://github.com/winhtaikaung/mintotp-ruby/blob/main/lib/mintotp/totp.rb) generates TOTP values from a
|
55
|
+
secret key and current time. Here is the entire code presented once again for convenience:
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
require 'base32'
|
59
|
+
require 'openssl'
|
60
|
+
|
61
|
+
module Mintotp
|
62
|
+
class TOTP
|
63
|
+
#
|
64
|
+
# @key : secret key
|
65
|
+
# @time_step : TimeStep Second Default is 30 Second
|
66
|
+
# @digits : Number of digits that will generate
|
67
|
+
# @digest : hash name
|
68
|
+
#
|
69
|
+
def totp(key, time_step=30, digits=6, digest='sha1')
|
70
|
+
hotp(key, Time.now.to_i / time_step, digits, digest)
|
71
|
+
end
|
72
|
+
|
73
|
+
def hotp(key, counter, digits=6, digest='sha1')
|
74
|
+
key = Base32.decode "#{key}#{"="* ((8-key.length)%8)}"
|
75
|
+
sha = OpenSSL::Digest::Digest.new(digest)
|
76
|
+
counter = [counter].pack('Q>')
|
77
|
+
hmac= OpenSSL::HMAC.digest(sha, key, counter)
|
78
|
+
|
79
|
+
offset = hmac.bytes.last & 0x0f
|
80
|
+
bytes = hmac.bytes[offset..offset+4]
|
81
|
+
|
82
|
+
# unpack by bytes shifting
|
83
|
+
bytes[0] = bytes[0] & 0x7f
|
84
|
+
bytes_as_integer = (bytes[0] << 24) + (bytes[1] << 16) + (bytes[2] << 8) + bytes[3]
|
85
|
+
bytes_as_integer.modulo(10 ** digits).to_s.rjust(digits,'0')
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
```
|
90
|
+
|
91
|
+
In the code above, we use the `hmac` module available in the Ruby
|
92
|
+
standard library to implement HOTP. The implementation can be found in
|
93
|
+
the `hotp()` function. It is a pretty straightforward implementation of
|
94
|
+
[RFC 2104: Section 5: HOTP Algorithm][RFC 2104-5]. It takes a
|
95
|
+
Base32-encoded secret key and a counter as input. It returns a 6-digit
|
96
|
+
HOTP value as output.
|
97
|
+
|
98
|
+
The `totp()` function implements the TOTP algorithm. It is a thin
|
99
|
+
wrapper around the HOTP algorithm. The TOTP value is obtained by
|
100
|
+
invoking the HOTP function with the secret key and the number of time
|
101
|
+
intervals (30 second intervals by default) that have elapsed since Unix
|
102
|
+
epoch (1970-01-01 00:00:00 UTC).
|
103
|
+
|
104
|
+
[RFC 2104-5]: https://tools.ietf.org/html/rfc4226#section-5
|
105
|
+
|
106
|
+
|
107
|
+
## Development
|
108
|
+
|
109
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
110
|
+
|
111
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
112
|
+
|
113
|
+
## Test
|
114
|
+
|
115
|
+
$ rspec
|
116
|
+
|
117
|
+
## Contributing
|
118
|
+
|
119
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/winhtaikaung/mintotp.
|
120
|
+
|
121
|
+
|
122
|
+
## License
|
123
|
+
|
124
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "mintotp"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/mintotp/totp.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'base32'
|
2
|
+
require 'openssl'
|
3
|
+
|
4
|
+
module Mintotp
|
5
|
+
class TOTP
|
6
|
+
#
|
7
|
+
# @example
|
8
|
+
# SECRET1 = "ZYTYYE5FOAGW5ML7LRWUL4WTZLNJAMZS"
|
9
|
+
# minTotp = Mintotp::TOTP.new()
|
10
|
+
# minTotp.totp(SECRET1)
|
11
|
+
# @param [string] secret key
|
12
|
+
# @param [integer] TimeStep Second Default is 30 Second in Google Authenticator
|
13
|
+
# @param [integer] Number of digits that will generate
|
14
|
+
# @param ['sha1'] : hash name
|
15
|
+
#
|
16
|
+
# @return [string] if so
|
17
|
+
def totp(key, time_step=30, digits=6, digest='sha1')
|
18
|
+
hotp(key, Time.now.to_i / time_step, digits, digest)
|
19
|
+
end
|
20
|
+
|
21
|
+
def hotp(key, counter, digits=6, digest='sha1')
|
22
|
+
key = Base32.decode "#{key}#{"="* ((8-key.length)%8)}"
|
23
|
+
sha = OpenSSL::Digest::Digest.new(digest)
|
24
|
+
counter = [counter].pack('Q>')
|
25
|
+
hmac= OpenSSL::HMAC.digest(sha, key, counter)
|
26
|
+
|
27
|
+
offset = hmac.bytes.last & 0x0f
|
28
|
+
bytes = hmac.bytes[offset..offset+4]
|
29
|
+
|
30
|
+
# unpack by bytes shifting
|
31
|
+
bytes[0] = bytes[0] & 0x7f
|
32
|
+
bytes_as_integer = (bytes[0] << 24) + (bytes[1] << 16) + (bytes[2] << 8) + bytes[3]
|
33
|
+
bytes_as_integer.modulo(10 ** digits).to_s.rjust(digits,'0')
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/lib/mintotp.rb
ADDED
data/mintotp.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require_relative 'lib/mintotp/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "mintotp"
|
5
|
+
spec.version = Mintotp::VERSION
|
6
|
+
spec.authors = ["win"]
|
7
|
+
spec.email = ["winhtaikaung28@hotmail.com"]
|
8
|
+
|
9
|
+
spec.summary = %q{Mintotp is a minimal TOTP generator written in Ruby}
|
10
|
+
spec.description = %q{TOTP stands for Time-Based One-Time Password. Many websites and services require two-factor authentication (2FA) or multi-factor authentication (MFA) where the user is required to present two or more pieces of evidence: Something only the user knows, e.g., password, passphrase, etc. Something only the user has, e.g., hardware token, mobile phone, etc. Something only the user is, e.g., biometrics. TOTP stands for Time-Based One-Time Password. Many websites and services require two-factor authentication (2FA) or multi-factor authentication (MFA) where the user is required to present two or more pieces of evidence. A TOTP value serves as the second factor, i.e., it proves that the user is in possession of a device (e.g., mobile phone) that contains a TOTP secret key from which the TOTP value is generated. Usually the service provider that provides a user's account also issues a secret key encoded either as a Base32 string or as a QR code. This secret key is added to an authenticator app (e.g., Google Authenticator) on a mobile device. The app can then generate TOTP values based on the current time. By default, it generates a new TOTP value every 30 seconds.}
|
11
|
+
spec.homepage = "https://github.com/winhtaikaung/mintotp-ruby"
|
12
|
+
spec.license = "MIT"
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
14
|
+
|
15
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
16
|
+
|
17
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
18
|
+
spec.metadata["source_code_uri"] = "https://github.com/winhtaikaung/mintotp-ruby"
|
19
|
+
spec.metadata["changelog_uri"] = "https://github.com/winhtaikaung/mintotp-ruby/CHANGELOG.md"
|
20
|
+
|
21
|
+
# Specify which files should be added to the gem when it is released.
|
22
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
23
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
24
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
25
|
+
end
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_runtime_dependency 'base32', '~> 0.3.2'
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mintotp
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- win
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-06-04 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: base32
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.3.2
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.3.2
|
27
|
+
description: 'TOTP stands for Time-Based One-Time Password. Many websites and services
|
28
|
+
require two-factor authentication (2FA) or multi-factor authentication (MFA) where
|
29
|
+
the user is required to present two or more pieces of evidence: Something only
|
30
|
+
the user knows, e.g., password, passphrase, etc. Something only the user has, e.g.,
|
31
|
+
hardware token, mobile phone, etc. Something only the user is, e.g., biometrics. TOTP
|
32
|
+
stands for Time-Based One-Time Password. Many websites and services require two-factor
|
33
|
+
authentication (2FA) or multi-factor authentication (MFA) where the user is required
|
34
|
+
to present two or more pieces of evidence. A TOTP value serves as the second factor,
|
35
|
+
i.e., it proves that the user is in possession of a device (e.g., mobile phone)
|
36
|
+
that contains a TOTP secret key from which the TOTP value is generated. Usually
|
37
|
+
the service provider that provides a user''s account also issues a secret key encoded
|
38
|
+
either as a Base32 string or as a QR code. This secret key is added to an authenticator
|
39
|
+
app (e.g., Google Authenticator) on a mobile device. The app can then generate TOTP
|
40
|
+
values based on the current time. By default, it generates a new TOTP value every
|
41
|
+
30 seconds.'
|
42
|
+
email:
|
43
|
+
- winhtaikaung28@hotmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".github/workflows/gem-test-push.yml"
|
49
|
+
- ".gitignore"
|
50
|
+
- ".rspec"
|
51
|
+
- Gemfile
|
52
|
+
- LICENSE.txt
|
53
|
+
- README.md
|
54
|
+
- Rakefile
|
55
|
+
- bin/console
|
56
|
+
- bin/setup
|
57
|
+
- lib/mintotp.rb
|
58
|
+
- lib/mintotp/totp.rb
|
59
|
+
- lib/mintotp/version.rb
|
60
|
+
- mintotp.gemspec
|
61
|
+
homepage: https://github.com/winhtaikaung/mintotp-ruby
|
62
|
+
licenses:
|
63
|
+
- MIT
|
64
|
+
metadata:
|
65
|
+
homepage_uri: https://github.com/winhtaikaung/mintotp-ruby
|
66
|
+
source_code_uri: https://github.com/winhtaikaung/mintotp-ruby
|
67
|
+
changelog_uri: https://github.com/winhtaikaung/mintotp-ruby/CHANGELOG.md
|
68
|
+
post_install_message:
|
69
|
+
rdoc_options: []
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: 2.3.0
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
requirements: []
|
83
|
+
rubygems_version: 3.1.6
|
84
|
+
signing_key:
|
85
|
+
specification_version: 4
|
86
|
+
summary: Mintotp is a minimal TOTP generator written in Ruby
|
87
|
+
test_files: []
|