bcrypt_pbkdf 1.0.0.alpha1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,78 @@
1
+ require 'minitest/autorun'
2
+ require 'test_helper'
3
+
4
+ # bcrypt_pbkdf in ruby using libsodium
5
+ require 'rbnacl/libsodium'
6
+ require 'rbnacl'
7
+ require 'rbnacl/hash'
8
+
9
+ BCRYPT_BLOCKS = 8
10
+ BCRYPT_HASHSIZE = BCRYPT_BLOCKS * 4
11
+
12
+ def bcrypt_pbkdf(password, salt, keylen, rounds)
13
+ stride = (keylen + BCRYPT_HASHSIZE - 1) / BCRYPT_HASHSIZE
14
+ amt = (keylen + stride - 1) / stride
15
+
16
+ sha2pass = RbNaCl::Hash.sha512(password)
17
+ #puts "[RB] sha2pass:#{sha2pass.inspect} #{sha2pass.size}"
18
+
19
+ remlen = keylen
20
+
21
+ countsalt = salt + "\x00"*4
22
+ saltlen = salt.size
23
+
24
+ key = "\x00"*keylen
25
+
26
+ # generate key in BCRYPT_HASHSIZE pieces
27
+ count = 1
28
+ while remlen > 0
29
+ countsalt[saltlen + 0] = ((count >> 24) & 0xff).chr
30
+ countsalt[saltlen + 1] = ((count >> 16) & 0xff).chr
31
+ countsalt[saltlen + 2] = ((count >> 8) & 0xff).chr
32
+ countsalt[saltlen + 3] = (count & 0xff).chr
33
+ #puts "[RC] countsalt: #{countsalt.inspect} len:#{countsalt.size}"
34
+
35
+ sha2salt = RbNaCl::Hash.sha512(countsalt)
36
+ tmpout = BCryptPbkdf::Engine::__bc_crypt_hash(sha2pass, sha2salt)
37
+ out = tmpout.clone
38
+
39
+ #puts "[RB] out: #{out.inspect} keylen:#{remlen} count:#{count}"
40
+ (1...rounds).each do |i|
41
+ sha2salt = RbNaCl::Hash.sha512(tmpout)
42
+ tmpout = BCryptPbkdf::Engine::__bc_crypt_hash(sha2pass, sha2salt)
43
+ out.bytes.each_with_index {|o,j| out.setbyte(j,o ^ tmpout[j].ord) }
44
+ end
45
+
46
+ amt = [amt, remlen].min
47
+ (0...amt).each do |i|
48
+ dest = i * stride + (count - 1)
49
+ key[dest] = out[i] if (dest < keylen)
50
+ end
51
+
52
+ remlen -= amt
53
+ count += 1
54
+ end
55
+ key
56
+ end
57
+
58
+
59
+ class TestExt < MiniTest::Unit::TestCase
60
+ def test_table
61
+ assert_equal table, table.map{ |p,s,l,r| [p,s,l,r,BCryptPbkdf::Engine::__bc_crypt_pbkdf(p,s,l,r).bytes] }
62
+ end
63
+ def test_ruby_and_native_returns_the_same
64
+ table.each do |p,s,l,r|
65
+ assert_equal bcrypt_pbkdf(p,s,l,r), BCryptPbkdf::Engine::__bc_crypt_pbkdf(p,s,l,r)
66
+ assert_equal bcrypt_pbkdf(p,s,l,r), BCryptPbkdf::key(p,s,l,r)
67
+ end
68
+ end
69
+
70
+
71
+ def table
72
+ [
73
+ ["pass2", "salt2", 12, 2, [214, 14, 48, 162, 131, 206, 121, 176, 50, 104, 231, 252]],
74
+ ["\u0000\u0001foo", "\u0001\u0002fooo3", 14, 5, [46, 189, 32, 185, 94, 85, 232, 10, 84, 26, 44, 161, 49, 126]],
75
+ ["doozoasd", "fooo$AS!", 14, 22, [57, 62, 50, 107, 70, 155, 65, 5, 129, 211, 189, 169, 188, 65]]
76
+ ]
77
+ end
78
+ end
@@ -0,0 +1,2 @@
1
+ $:.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'bcrypt_pbkdf'
metadata ADDED
@@ -0,0 +1,163 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bcrypt_pbkdf
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0.alpha1
5
+ platform: ruby
6
+ authors:
7
+ - Miklos Fazekas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake-compiler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.9.7
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.9.7
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rbnacl
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rbnacl-libsodium
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 1.0.8
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 1.0.8
69
+ - !ruby/object:Gem::Dependency
70
+ name: rdoc
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.12'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.12'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake-compiler-dock
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.5.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.5.0
97
+ description: " This gem implements bcrypt_pdkfd (a variant of PBKDF2 with bcrypt-based
98
+ PRF)\n"
99
+ email: mfazekas@szemafor.com
100
+ executables: []
101
+ extensions:
102
+ - ext/mri/extconf.rb
103
+ extra_rdoc_files:
104
+ - README.md
105
+ - COPYING
106
+ - CHANGELOG.md
107
+ - lib/bcrypt_pbkdf.rb
108
+ files:
109
+ - ".travis.yml"
110
+ - CHANGELOG.md
111
+ - COPYING
112
+ - Gemfile
113
+ - Gemfile.lock
114
+ - README.md
115
+ - Rakefile
116
+ - bcrypt_pbkdf.gemspec
117
+ - ext/mri/bcrypt_pbkdf.c
118
+ - ext/mri/bcrypt_pbkdf_ext.c
119
+ - ext/mri/blf.h
120
+ - ext/mri/blowfish.c
121
+ - ext/mri/crypto_api.h
122
+ - ext/mri/crypto_hash_sha512.h
123
+ - ext/mri/explicit_bzero.c
124
+ - ext/mri/extconf.rb
125
+ - ext/mri/hash_sha512.c
126
+ - ext/mri/includes.h
127
+ - ext/mri/sha2.h
128
+ - ext/mri/util.h
129
+ - ext/mri/utils.h
130
+ - lib/bcrypt_pbkdf.rb
131
+ - test/bcrypt_pnkdf/engine_test.rb
132
+ - test/test_helper.rb
133
+ homepage: https://github.com/mfazekas/bcrypt_pbkdf-ruby
134
+ licenses:
135
+ - MIT
136
+ metadata: {}
137
+ post_install_message:
138
+ rdoc_options:
139
+ - "--title"
140
+ - bcrypt_pbkdf
141
+ - "--line-numbers"
142
+ - "--inline-source"
143
+ - "--main"
144
+ - README.md
145
+ require_paths:
146
+ - lib
147
+ required_ruby_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ required_rubygems_version: !ruby/object:Gem::Requirement
153
+ requirements:
154
+ - - ">"
155
+ - !ruby/object:Gem::Version
156
+ version: 1.3.1
157
+ requirements: []
158
+ rubyforge_project:
159
+ rubygems_version: 2.5.1
160
+ signing_key:
161
+ specification_version: 4
162
+ summary: OpenBSD's bcrypt_pdkfd (a variant of PBKDF2 with bcrypt-based PRF)
163
+ test_files: []