bitauth 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.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +72 -0
- data/Rakefile +2 -0
- data/bitauth.gemspec +23 -0
- data/lib/bitauth.rb +91 -0
- metadata +81 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 7257cdd3cf9e87a4ce19e5bc4180f13977494803
|
|
4
|
+
data.tar.gz: c6bba4b5ab3e80ac5d1835177758456cafd22f8c
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: d09c17bb0af327328edec18571c7e76055c55fa3e895ad9b9738f0ccf9a2c965ff618d2e4f9c80044b15008e7d8d1ae6e8cf3104de6ea90eec9be65b85e97cc2
|
|
7
|
+
data.tar.gz: 71423c67acbe4e9c802b8d03743d7593bf2336c8f2ddb03144d3db0e7e59bb249e1edacbc8a940e3ea39e5017b7d772735c98742484a3ff97fdd901a3535b633
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2015 Gabriel Manricks
|
|
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.
|
data/README.md
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# Bitauth
|
|
2
|
+
|
|
3
|
+
BitAuth is a way to do secure, passwordless authentication proposed by Bitpay using the same elliptic-curve cryptography as Bitcoin
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem 'bitauth'
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
And then execute:
|
|
14
|
+
|
|
15
|
+
$ bundle
|
|
16
|
+
|
|
17
|
+
Or install it yourself as:
|
|
18
|
+
|
|
19
|
+
$ gem install bitauth
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
To Generate a new Key pair you can just instantiate a new BitAuth instance with no parameters:
|
|
24
|
+
|
|
25
|
+
```ruby
|
|
26
|
+
require 'bitauth'
|
|
27
|
+
|
|
28
|
+
bitauth = BitAuth.new
|
|
29
|
+
|
|
30
|
+
bitauth.sin
|
|
31
|
+
bitauth.public_key
|
|
32
|
+
bitauth.private_key
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
To sign a message after that you can just call `sign`:
|
|
36
|
+
|
|
37
|
+
```ruby
|
|
38
|
+
bitauth = BitAuth.new private_key: "HEX_PRIVATE_KEY"
|
|
39
|
+
|
|
40
|
+
signature = bitauth.sign("data")
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
You can then verify the message using just the public key:
|
|
44
|
+
|
|
45
|
+
```ruby
|
|
46
|
+
bitauth = BitAuth.new public_key: "HEX_PUBLIC_KEY"
|
|
47
|
+
|
|
48
|
+
bitauth.verify("data", "signature")
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Example
|
|
52
|
+
|
|
53
|
+
```ruby
|
|
54
|
+
require 'bitauth'
|
|
55
|
+
|
|
56
|
+
bitauth = BitAuth.new
|
|
57
|
+
|
|
58
|
+
data = '{"id":10, "message": "Demo Text"}'
|
|
59
|
+
signature = bitauth.sign(data)
|
|
60
|
+
|
|
61
|
+
if bitauth.verify(data, signature)
|
|
62
|
+
puts "Everything checks out"
|
|
63
|
+
end
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Contributing
|
|
67
|
+
|
|
68
|
+
1. Fork it ( https://github.com/gmanricks/BitAuth/fork )
|
|
69
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
70
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
|
71
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
|
72
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bitauth.gemspec
ADDED
|
@@ -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 'bitauth'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "bitauth"
|
|
8
|
+
spec.version = BitAuth::VERSION
|
|
9
|
+
spec.authors = ["Gabriel Manricks"]
|
|
10
|
+
spec.email = ["gmanricks@gmail.com"]
|
|
11
|
+
spec.summary = "Passwordless authentication protocol based on the same cryptography used in the Bitcoin protocol"
|
|
12
|
+
spec.description = "BitAuth is a way to do secure, passwordless authentication proposed by Bitpay using the same elliptic-curve cryptography as Bitcoin"
|
|
13
|
+
spec.homepage = "https://github.com/gmanricks/BitAuth"
|
|
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", "~> 10.0"
|
|
23
|
+
end
|
data/lib/bitauth.rb
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
require "openssl"
|
|
2
|
+
|
|
3
|
+
class BitAuth
|
|
4
|
+
VERSION = "0.0.2"
|
|
5
|
+
|
|
6
|
+
attr_accessor :sin, :public_key, :private_key
|
|
7
|
+
|
|
8
|
+
def initialize(opts = {})
|
|
9
|
+
if opts[:private_key]
|
|
10
|
+
self.private_key = opts[:private_key]
|
|
11
|
+
self.public_key = pubkey_from_privkey private_key
|
|
12
|
+
self.sin = generate_sin public_key
|
|
13
|
+
elsif opts[:public_key]
|
|
14
|
+
self.public_key = opts[:public_key]
|
|
15
|
+
self.sin = generate_sin public_key
|
|
16
|
+
else
|
|
17
|
+
generate_keys
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def sign(data)
|
|
22
|
+
fail(ArgumentError, 'Missing Private Key') if public_key.nil?
|
|
23
|
+
|
|
24
|
+
hash = OpenSSL::Digest::SHA256.new.hexdigest data
|
|
25
|
+
|
|
26
|
+
ecdsa = OpenSSL::PKey::EC.new('secp256k1')
|
|
27
|
+
ecdsa.private_key = OpenSSL::BN.new private_key, 16
|
|
28
|
+
|
|
29
|
+
ecdsa.dsa_sign_asn1(hash).unpack('H*')[0]
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def verify(data, sig)
|
|
33
|
+
hash = OpenSSL::Digest::SHA256.new.hexdigest data
|
|
34
|
+
|
|
35
|
+
ecdsa = OpenSSL::PKey::EC.new('secp256k1')
|
|
36
|
+
pub = OpenSSL::PKey::EC::Point.new(ecdsa.group, OpenSSL::BN.new(public_key, 16))
|
|
37
|
+
|
|
38
|
+
ecdsa.public_key = pub
|
|
39
|
+
ecdsa.dsa_verify_asn1(hash, [sig].pack('H*'))
|
|
40
|
+
rescue
|
|
41
|
+
false
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
private
|
|
45
|
+
|
|
46
|
+
def base58(num)
|
|
47
|
+
letters = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
|
|
48
|
+
res = ''
|
|
49
|
+
while num > 0
|
|
50
|
+
num, r = num.divmod(58)
|
|
51
|
+
res.prepend letters[r]
|
|
52
|
+
end
|
|
53
|
+
res
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def generate_sin(pubkey)
|
|
57
|
+
sha = OpenSSL::Digest::SHA256.new
|
|
58
|
+
ripe = OpenSSL::Digest::RIPEMD160.new
|
|
59
|
+
|
|
60
|
+
hash = '0f02' + ripe.hexdigest(sha.digest([pubkey].pack('H*')))
|
|
61
|
+
checksum = sha.hexdigest(sha.digest([hash].pack('H*')))[0...8]
|
|
62
|
+
|
|
63
|
+
key = hash + checksum
|
|
64
|
+
base58 key.hex
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def pubkey_from_privkey(privkey)
|
|
68
|
+
ecdsa = OpenSSL::PKey::EC.new('secp256k1')
|
|
69
|
+
priv = OpenSSL::BN.new(privkey, 16)
|
|
70
|
+
|
|
71
|
+
pub = ecdsa.group.generator.mul([priv], []).to_bn.to_i.to_s(16).rjust(130, '0')
|
|
72
|
+
|
|
73
|
+
x = [pub].pack('H*')[1..32].unpack('H*').first
|
|
74
|
+
y = [pub].pack('H*')[33..64].unpack('H*').first.to_i(16)
|
|
75
|
+
|
|
76
|
+
if y.odd?
|
|
77
|
+
'03' + x
|
|
78
|
+
else
|
|
79
|
+
'02' + x
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def generate_keys
|
|
84
|
+
ecdsa = OpenSSL::PKey::EC.new('secp256k1')
|
|
85
|
+
keys = ecdsa.generate_key
|
|
86
|
+
|
|
87
|
+
self.private_key = keys.private_key.to_s.to_i.to_s(16)
|
|
88
|
+
self.public_key = pubkey_from_privkey private_key
|
|
89
|
+
self.sin = generate_sin public_key
|
|
90
|
+
end
|
|
91
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: bitauth
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.2
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Gabriel Manricks
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2015-02-17 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: '10.0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - ~>
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '10.0'
|
|
41
|
+
description: BitAuth is a way to do secure, passwordless authentication proposed by
|
|
42
|
+
Bitpay using the same elliptic-curve cryptography as Bitcoin
|
|
43
|
+
email:
|
|
44
|
+
- gmanricks@gmail.com
|
|
45
|
+
executables: []
|
|
46
|
+
extensions: []
|
|
47
|
+
extra_rdoc_files: []
|
|
48
|
+
files:
|
|
49
|
+
- .gitignore
|
|
50
|
+
- Gemfile
|
|
51
|
+
- LICENSE.txt
|
|
52
|
+
- README.md
|
|
53
|
+
- Rakefile
|
|
54
|
+
- bitauth.gemspec
|
|
55
|
+
- lib/bitauth.rb
|
|
56
|
+
homepage: https://github.com/gmanricks/BitAuth
|
|
57
|
+
licenses:
|
|
58
|
+
- MIT
|
|
59
|
+
metadata: {}
|
|
60
|
+
post_install_message:
|
|
61
|
+
rdoc_options: []
|
|
62
|
+
require_paths:
|
|
63
|
+
- lib
|
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - '>='
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '0'
|
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
70
|
+
requirements:
|
|
71
|
+
- - '>='
|
|
72
|
+
- !ruby/object:Gem::Version
|
|
73
|
+
version: '0'
|
|
74
|
+
requirements: []
|
|
75
|
+
rubyforge_project:
|
|
76
|
+
rubygems_version: 2.2.2
|
|
77
|
+
signing_key:
|
|
78
|
+
specification_version: 4
|
|
79
|
+
summary: Passwordless authentication protocol based on the same cryptography used
|
|
80
|
+
in the Bitcoin protocol
|
|
81
|
+
test_files: []
|