secondfactor 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7e9b1ac431c194b98785d7fadb0354d47abfc4e3
4
+ data.tar.gz: 0cd5e7bb80c1049947f1fc8b177b363b54f689e2
5
+ SHA512:
6
+ metadata.gz: 16f3ed8ada3fb918998caffe561154e6ceec9c74219a2e64dc0302d60f817b4fc631639472d2192074d45cd61d8497929979c90d007a208640211ab9cee2fbf7
7
+ data.tar.gz: '08c03ff1b2b1f66b750892ad5e0004c174f983f782e974b0e6694db2b54c4ea26c2bdadca7966a1aa4a07ba4d4a87cd70c5b0d05dd90a801e6914c4da992daad'
data/LICENSE ADDED
@@ -0,0 +1,9 @@
1
+ Copyright 2017 Elliot Speck
2
+
3
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4
+
5
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6
+
7
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
+
9
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
File without changes
@@ -0,0 +1,9 @@
1
+ module SecondFactor
2
+ class HOTP
3
+ def self.generate(secret_based, step)
4
+ hmac = SecondFactor::OTP.generate_hmac(secret_based, step)
5
+ hotp = (hmac % 10 ** 6).to_s.rjust(6, '0')
6
+ return hotp
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,35 @@
1
+ module SecondFactor
2
+ class OTP
3
+ def self.generate_seed(length=10)
4
+ seed_bytes = (0...length).map { rand(255).chr }
5
+ seed_based = Base32.encode(seed_bytes.join)
6
+
7
+ return seed_based
8
+ end
9
+
10
+ def self.generate_hmac(seed_based, step)
11
+ seed_bytes = Base32.decode(seed_based)
12
+ hmac = OpenSSL::HMAC.digest(OpenSSL::Digest.new("sha1"), seed_bytes, intbytes(step))
13
+
14
+ # https://tools.ietf.org/html/rfc4226#section-5.4
15
+ # What's security without a bit of math I don't understand, right?
16
+ # Lucky that RFC is easy to understand...
17
+ offset = hmac[-1].ord & 0xF
18
+ truncated = (hmac[offset].ord & 0x7F) << 24 | (hmac[offset + 1].ord & 0xFF) << 16 | (hmac[offset + 2].ord & 0xFF) << 8 | (hmac[offset + 3].ord & 0xFF)
19
+
20
+ return truncated
21
+ end
22
+
23
+ # Roughly adapted from github.com/aeyris/otp
24
+ def intbytes(int)
25
+ result = ""
26
+
27
+ 8.times do
28
+ result << (int & 0xFF).chr
29
+ int >>= 8
30
+ end
31
+
32
+ return result.reverse.rjust(8, 0.chr)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,13 @@
1
+ module SecondFactor
2
+ class TOTP
3
+ def self.generate(secret_based)
4
+ now_step = Time.now.to_i / 30
5
+
6
+ return [
7
+ SecondFactor::HOTP.generate(secret_based, now_step.pred),
8
+ SecondFactor::HOTP.generate(secret_based, now_step),
9
+ SecondFactor::HOTP.generate(secret_based, now_step.succ)
10
+ ]
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module SecondFactor
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,9 @@
1
+ require 'openssl'
2
+ require 'base32'
3
+ require 'secondfactor/version'
4
+ require 'secondfactor/otp'
5
+ require 'secondfactor/hotp'
6
+ require 'secondfactor/totp'
7
+
8
+ module SecondFactor
9
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: secondfactor
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Elliot Speck
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-02-25 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple HMAC-based and time-based two-factor authentication library.
14
+ email: rubygems@elliot.pro
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - LICENSE
20
+ - README.md
21
+ - lib/secondfactor.rb
22
+ - lib/secondfactor/hotp.rb
23
+ - lib/secondfactor/otp.rb
24
+ - lib/secondfactor/totp.rb
25
+ - lib/secondfactor/version.rb
26
+ homepage: https://github.com/aeyris/secondfactor
27
+ licenses:
28
+ - BSD-2-Clause
29
+ metadata: {}
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 2.6.8
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: A simple HMAC-based and time-based two-factor authentication library.
50
+ test_files: []