otp 0.0.2

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9c347957dce5fa1a828f04c97e6c4ad600a06e8a
4
+ data.tar.gz: 3bd57768dd62dbcf301115d62ff7957ff6a06395
5
+ SHA512:
6
+ metadata.gz: ff936f0e8dd58332943d6871f8df6f7eaa7f270253dbecd8ec3bf652d57dbabd23b5d162bdcee77e7ac88f6d58afcff58a99efade8b21f0e68808f89f7c29c36
7
+ data.tar.gz: e1270fd1fdfcf1c68164159758c2eac6325ec5144c3974f315e59cfc38cc876f4664654cb352e5ca105f78d69ee27cfd05a2a1eaf8968c977e589af1178711e2
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in otp.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Yuuzou Gotou
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,38 @@
1
+ # One-Time Password Library
2
+
3
+ This library provides an implementation of
4
+ HMAC-Based One-Time Password Algorithm (HOTP; RFC4226) and
5
+ Time-Based One-Time Password Algorithm (HOTP; RFC6238).
6
+ The Algorithm details can be referred at the following URLs.
7
+
8
+ * HOTP: http://tools.ietf.org/html/rfc4226
9
+ * TOTP: http://tools.ietf.org/html/rfc6238
10
+
11
+ ## Usage
12
+
13
+ To create new TOTP secret:
14
+
15
+ require "otp"
16
+
17
+ totp = OTP::TOTP.new
18
+ totp.new_secret # create random secret
19
+ p totp.secret #=> "YVMR2G7N4OAXGKFC" (BASE32-formated HMAC key)
20
+ p totp.algorithm #=> "SHA1" (HMAC algorithm; default SHA1)
21
+ p totp.digits #=> 6 (number of password digits; default 6)
22
+ p totp.period #=> 30 (time step period in second; default 30)
23
+ p totp.time #=> nil (UNIX time by Time or Integer; nil for the current time)
24
+ p totp.password #=> "123456" (password for the current time)
25
+
26
+ To verify given TOTP password:
27
+
28
+ require "otp"
29
+
30
+ totp = OTP::TOTP.new
31
+ totp.secret = "YVMR2G7N4OAXGKFC"
32
+ p totp.verify("123456") #=> true/false (verify given passowrd)
33
+
34
+ The value of "secret" is encoded with BASE32 algorithm to be compatible
35
+ with Google Authenticator URI format.
36
+
37
+ * BASE32: http://tools.ietf.org/html/rfc4648
38
+ * Google Authenticator Key URI format: https://github.com/google/google-authenticator/wiki/Key-Uri-Format
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |test|
5
+ test.pattern = "test/**/test_*.rb"
6
+ test.verbose = true
7
+ end
8
+
9
+ task :default => :test
@@ -0,0 +1,3 @@
1
+ require "otp/version"
2
+ require "otp/totp"
3
+ require "otp/hotp"
@@ -0,0 +1,44 @@
1
+ require "otp/utils"
2
+ require "otp/base32"
3
+
4
+ module OTP
5
+ class Base
6
+ include OTP::Utils
7
+
8
+ attr_accessor :secret
9
+ attr_accessor :algorithm
10
+ attr_accessor :digits
11
+
12
+ def initialize(secret=nil, algorithm="SHA1", digits=6)
13
+ self.secret = secret
14
+ self.algorithm = algorithm
15
+ self.digits = digits
16
+ end
17
+
18
+ def new_secret(num_bytes=10)
19
+ s = (0...num_bytes).map{ Random.rand(256).chr }.join
20
+ self.secret = OTP::Base32.encode(s)
21
+ end
22
+
23
+ def moving_factor
24
+ raise NotImplementedError
25
+ end
26
+
27
+ def otp
28
+ hash = hmac(algorithm, OTP::Base32.decode(secret),
29
+ pack_int64(moving_factor))
30
+ return truncate(hash)
31
+ end
32
+
33
+ def password
34
+ pw = (otp % (10 ** digits)).to_s
35
+ pw = "0" + pw while pw.length < digits
36
+ return pw
37
+ end
38
+
39
+ def verify(otp)
40
+ return false if otp.nil? || otp.empty?
41
+ return compare(password, otp)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,62 @@
1
+ module OTP
2
+ module Base32
3
+ BASE32_ENCODE = %w(
4
+ A B C D E F G H I J K L M N O P
5
+ Q R S T U V W X Y Z 2 3 4 5 6 7
6
+ )
7
+
8
+ BASE32_DECODE = {
9
+ ?A=>0, ?B=>1, ?C=>2, ?D=>3, ?E=>4, ?F=>5, ?G=>6, ?H=>7,
10
+ ?I=>8, ?J=>9, ?K=>10, ?L=>11, ?M=>12, ?N=>13, ?O=>14, ?P=>15,
11
+ ?Q=>16, ?R=>17, ?S=>18, ?T=>19, ?U=>20, ?V=>21, ?W=>22, ?X=>23,
12
+ ?Y=>24, ?Z=>25, ?2=>26, ?3=>27, ?4=>28, ?5=>29, ?6=>30, ?7=>31,
13
+ ?==>-1,
14
+ }
15
+
16
+ module_function
17
+
18
+ def encode(bytes)
19
+ return nil unless bytes
20
+ ret = ""
21
+ bytes = bytes.dup.force_encoding("binary")
22
+ off = 0
23
+ while off < bytes.length
24
+ n = 0
25
+ bits = bytes[off, 5]
26
+ l = bits.length
27
+ bits << "\0" while bits.length < 5
28
+ bits.each_byte{|b| n = (n << 8) | b }
29
+ 8.times do |i|
30
+ ret << ((l*8/5 < i) ? ?= : BASE32_ENCODE[(n >> (7-i)*5) & 0x1f])
31
+ end
32
+ off += 5
33
+ end
34
+ return ret
35
+ end
36
+
37
+ def decode(chars)
38
+ return nil unless chars
39
+ ret = ""
40
+ chars = chars.upcase
41
+ ret.force_encoding("binary")
42
+ off = 0
43
+ while off < chars.length
44
+ n = l = 0
45
+ bits = chars[off, 8]
46
+ bits << "=" while bits.length < 8
47
+ bits.each_char.with_index do |c, i|
48
+ d = BASE32_DECODE[c]
49
+ raise ArgumentError, "invalid char: #{c}" unless d
50
+ n <<= 5
51
+ if d >= 0
52
+ n |= d
53
+ l = i * 5 / 8
54
+ end
55
+ end
56
+ ret << (0..l).map{|i| (n >> 32 - i * 8) & 0xff }.pack("c*")
57
+ off += 8
58
+ end
59
+ return ret
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,16 @@
1
+ require "otp/base"
2
+
3
+ module OTP
4
+ class HOTP < OTP::Base
5
+ attr_accessor :count
6
+
7
+ def initialize(*args)
8
+ super
9
+ self.count = 0
10
+ end
11
+
12
+ def moving_factor
13
+ return count
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ require "otp/base"
2
+
3
+ module OTP
4
+ class TOTP < OTP::Base
5
+ attr_accessor :period, :time
6
+
7
+ def initialize(*args)
8
+ super
9
+ self.period = 30
10
+ self.time = nil
11
+ end
12
+
13
+ def moving_factor
14
+ return (time || Time.now).to_i / period
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,27 @@
1
+ require "openssl"
2
+
3
+ module OTP
4
+ module Utils
5
+ private
6
+
7
+ def pack_int64(i)
8
+ return [i >> 32 & 0xffffffff, i & 0xffffffff].pack("NN")
9
+ end
10
+
11
+ def hmac(algorithm, secret, text)
12
+ mac = OpenSSL::HMAC.new(secret, algorithm)
13
+ mac << text
14
+ return mac.digest
15
+ end
16
+
17
+ def truncate(hash)
18
+ offset = hash[-1].ord & 0xf
19
+ binary = hash[offset, 4]
20
+ return binary.unpack("N")[0] & 0x7fffffff
21
+ end
22
+
23
+ def compare(a, b)
24
+ return a.to_i == b.to_i
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,4 @@
1
+ module OTP
2
+ VERSION = "0.0.2"
3
+ end
4
+
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'otp/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "otp"
8
+ spec.version = OTP::VERSION
9
+ spec.authors = ["Yuuzou Gotou"]
10
+ spec.email = ["gotoyuzo@notwork.org"]
11
+ spec.summary = %q{One-Time Password Library}
12
+ spec.description = %q{An implementation of HOTP (RFC4226) and TOTP (RFC6238).}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,30 @@
1
+ require "test/unit"
2
+ require "otp/base32"
3
+
4
+ class TestBase32 < Test::Unit::TestCase
5
+ def assert_encode(plain, encoded)
6
+ assert_equal(::OTP::Base32.encode(plain), encoded)
7
+ end
8
+
9
+ def assert_decode(encoded, plain)
10
+ plain &&= plain.dup.force_encoding("binary")
11
+ assert_equal(::OTP::Base32.decode(encoded), plain)
12
+ end
13
+
14
+ def assert_encode_decode(plain, encoded)
15
+ assert_encode(plain, encoded)
16
+ assert_decode(encoded, plain)
17
+ end
18
+
19
+ def test_base32
20
+ assert_encode_decode(nil, nil)
21
+ assert_encode_decode("", "")
22
+ assert_encode_decode("f", "MY======")
23
+ assert_encode_decode("fo", "MZXQ====")
24
+ assert_encode_decode("foo", "MZXW6===")
25
+ assert_encode_decode("foob", "MZXW6YQ=")
26
+ assert_encode_decode("fooba", "MZXW6YTB")
27
+ assert_encode_decode("foobar", "MZXW6YTBOI======")
28
+ assert_encode_decode("\u{3042}\u{3044}\u{3046}\u{3048}\u{304a}", "4OAYFY4BQTRYDBXDQGEOHAMK")
29
+ end
30
+ end
@@ -0,0 +1,26 @@
1
+ require "test/unit"
2
+ require "otp"
3
+
4
+ class TestHTOP < Test::Unit::TestCase
5
+ def assert_hotp(hotp, count, pass)
6
+ hotp.count = count
7
+ assert_equal(hotp.password, pass)
8
+ assert(hotp.verify(pass))
9
+ assert(!hotp.verify(pass.chop))
10
+ end
11
+
12
+ def test_hotp
13
+ seed = "12345678901234567890"
14
+ hotp = OTP::HOTP.new(OTP::Base32.encode(seed), "SHA1", 6)
15
+ assert_hotp(hotp, 0, "755224")
16
+ assert_hotp(hotp, 1, "287082")
17
+ assert_hotp(hotp, 2, "359152")
18
+ assert_hotp(hotp, 3, "969429")
19
+ assert_hotp(hotp, 4, "338314")
20
+ assert_hotp(hotp, 5, "254676")
21
+ assert_hotp(hotp, 6, "287922")
22
+ assert_hotp(hotp, 7, "162583")
23
+ assert_hotp(hotp, 8, "399871")
24
+ assert_hotp(hotp, 9, "520489")
25
+ end
26
+ end
@@ -0,0 +1,44 @@
1
+ require "test/unit"
2
+ require "otp"
3
+
4
+ class TestTOTP < Test::Unit::TestCase
5
+ def assert_totp(totp, time, pass)
6
+ totp.time = time
7
+ assert_equal(totp.password, pass)
8
+ assert(totp.verify(pass))
9
+ assert(!totp.verify(pass.chop))
10
+ end
11
+
12
+ def test_totp_sha1
13
+ seed = "12345678901234567890"
14
+ totp = OTP::TOTP.new(OTP::Base32.encode(seed), "SHA1", 8)
15
+ assert_totp(totp, 59, "94287082")
16
+ assert_totp(totp, 1111111109, "07081804")
17
+ assert_totp(totp, 1111111111, "14050471")
18
+ assert_totp(totp, 1234567890, "89005924")
19
+ assert_totp(totp, 2000000000, "69279037")
20
+ assert_totp(totp, 20000000000, "65353130")
21
+ end
22
+
23
+ def test_totp_sha256
24
+ seed = "12345678901234567890123456789012"
25
+ totp = OTP::TOTP.new(OTP::Base32.encode(seed), "SHA256", 8)
26
+ assert_totp(totp, 59, "46119246")
27
+ assert_totp(totp, 1111111109, "68084774")
28
+ assert_totp(totp, 1111111111, "67062674")
29
+ assert_totp(totp, 1234567890, "91819424")
30
+ assert_totp(totp, 2000000000, "90698825")
31
+ assert_totp(totp, 20000000000, "77737706")
32
+ end
33
+
34
+ def test_totp_sha512
35
+ seed = "1234567890123456789012345678901234567890123456789012345678901234"
36
+ totp = OTP::TOTP.new(OTP::Base32.encode(seed), "SHA512", 8)
37
+ assert_totp(totp, 59, "90693936")
38
+ assert_totp(totp, 1111111109, "25091201")
39
+ assert_totp(totp, 1111111111, "99943326")
40
+ assert_totp(totp, 1234567890, "93441116")
41
+ assert_totp(totp, 2000000000, "38618901")
42
+ assert_totp(totp, 20000000000, "47863826")
43
+ end
44
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: otp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Yuuzou Gotou
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: An implementation of HOTP (RFC4226) and TOTP (RFC6238).
42
+ email:
43
+ - gotoyuzo@notwork.org
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - lib/otp.rb
54
+ - lib/otp/base.rb
55
+ - lib/otp/base32.rb
56
+ - lib/otp/hotp.rb
57
+ - lib/otp/totp.rb
58
+ - lib/otp/utils.rb
59
+ - lib/otp/version.rb
60
+ - otp.gemspec
61
+ - test/test_base32.rb
62
+ - test/test_hotp.rb
63
+ - test/test_totp.rb
64
+ homepage: ''
65
+ licenses:
66
+ - MIT
67
+ metadata: {}
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: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 2.2.2
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: One-Time Password Library
88
+ test_files:
89
+ - test/test_base32.rb
90
+ - test/test_hotp.rb
91
+ - test/test_totp.rb