crypto-lite 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +3 -0
- data/Manifest.txt +11 -0
- data/README.md +10 -0
- data/Rakefile +29 -0
- data/lib/crypto-lite.rb +82 -0
- data/lib/crypto-lite/version.rb +23 -0
- data/lib/crypto.rb +2 -0
- data/lib/crypto/lite.rb +2 -0
- data/test/helper.rb +11 -0
- data/test/test_hash.rb +78 -0
- data/test/test_version.rb +19 -0
- metadata +94 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6408a018c374d22c0aabb5c532ec5ad7a32c6498d9d387d5e5042849c9d82dc5
|
4
|
+
data.tar.gz: 30c6920e588bcbc908f92ae72d506c74c668079570222d60e7b7d8a306e3846e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8c81352f2acfa641de7b6eaebbf6b0c520f0439db3be966245dd713ede27bcc2da97746e6966e9412fc2a05bc552948189fe3fa5bb02c71b0a822347c6211cda
|
7
|
+
data.tar.gz: 2b6779c4a6234d21256ad260f9949e4951c5b940f91e25a749bcffd0fed9640e9c43bfd06e578f03ba254ee290d5cf08db9d3e58ace927312ae53aafca2629b3
|
data/CHANGELOG.md
ADDED
data/Manifest.txt
ADDED
data/README.md
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'hoe'
|
2
|
+
require './lib/crypto-lite/version.rb'
|
3
|
+
|
4
|
+
Hoe.spec 'crypto-lite' do
|
5
|
+
|
6
|
+
self.version = CryptoLite::VERSION
|
7
|
+
|
8
|
+
self.summary = "crypto-lite - Cryptographic Secure Hash Functions and Public Key Signature Algorithms Made Easy"
|
9
|
+
self.description = summary
|
10
|
+
|
11
|
+
self.urls = { home: 'https://github.com/rubycoco/blockchain' }
|
12
|
+
|
13
|
+
self.author = 'Gerald Bauer'
|
14
|
+
self.email = 'wwwmake@googlegroups.com'
|
15
|
+
|
16
|
+
# switch extension to .markdown for gihub formatting
|
17
|
+
self.readme_file = 'README.md'
|
18
|
+
self.history_file = 'CHANGELOG.md'
|
19
|
+
|
20
|
+
self.extra_deps = [
|
21
|
+
]
|
22
|
+
|
23
|
+
self.licenses = ['Public Domain']
|
24
|
+
|
25
|
+
self.spec_extras = {
|
26
|
+
required_ruby_version: '>= 2.3'
|
27
|
+
}
|
28
|
+
|
29
|
+
end
|
data/lib/crypto-lite.rb
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'pp'
|
2
|
+
require 'digest'
|
3
|
+
require 'openssl'
|
4
|
+
|
5
|
+
|
6
|
+
## our own code
|
7
|
+
require 'crypto-lite/version' # note: let version always go first
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
module Crypto
|
12
|
+
|
13
|
+
def self.sha256bin( input, engine=nil ) ## todo/check: add alias sha256b or such to - why? why not?
|
14
|
+
input_type = if input.is_a?( String )
|
15
|
+
"#{input.class.name}/#{input.encoding}"
|
16
|
+
else
|
17
|
+
input.class.name
|
18
|
+
end
|
19
|
+
puts " input: #{input} (#{input_type})"
|
20
|
+
|
21
|
+
message = if input.is_a?( Integer ) ## assume byte if single (unsigned) integer
|
22
|
+
raise ArgumentError, "expected unsigned byte (0-255) - got #{input} (0x#{input.to_s(16)}) - can't pack negative number; sorry" if input < 0
|
23
|
+
## note: pack - H (String) => hex string (high nibble first)
|
24
|
+
## todo/check: is there a better way to convert integer number to (binary) string!!!
|
25
|
+
[input.to_s(16)].pack('H*')
|
26
|
+
else ## assume (binary) string
|
27
|
+
input
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
bytes = message.bytes
|
32
|
+
bin = bytes.map {|byte| byte.to_s(2).rjust(8, "0")}.join( ' ' )
|
33
|
+
hex = bytes.map {|byte| byte.to_s(16).rjust(2, "0")}.join( ' ' )
|
34
|
+
puts " #{pluralize( bytes.size, 'byte')}: #{bytes.inspect}"
|
35
|
+
puts " binary: #{bin}"
|
36
|
+
puts " hex: #{hex}"
|
37
|
+
|
38
|
+
if engine && ['openssl'].include?( engine.to_s.downcase )
|
39
|
+
puts " engine: #{engine}"
|
40
|
+
digest = OpenSSL::Digest::SHA256.new
|
41
|
+
digest.update( message )
|
42
|
+
digest.digest
|
43
|
+
else ## use "built-in" hash function from digest module
|
44
|
+
Digest::SHA256.digest( message )
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.sha256( input, engine=nil )
|
49
|
+
sha256bin( input, engine ).unpack( 'H*' )[0]
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
def self.sha256hex( input, engine=nil )
|
54
|
+
## convenience helper - lets you pass in hex string
|
55
|
+
|
56
|
+
## check if input starts with 0x or 0X if yes - (auto-)cut off!!!!!
|
57
|
+
if input.start_with?( '0x') || input.start_with?( '0X' )
|
58
|
+
input = input[2..-1]
|
59
|
+
end
|
60
|
+
|
61
|
+
raise ArgumentError, "expected hex string (0-9a-f) - got >#{input}< - can't pack string; sorry" if input.downcase =~ /[^0-9a-f]/
|
62
|
+
sha256( [input].pack( 'H*' ), engine )
|
63
|
+
end
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
def self.pluralize( count, noun )
|
68
|
+
count == 1 ? "#{count} #{noun}" : "#{count} #{noun}s"
|
69
|
+
end
|
70
|
+
|
71
|
+
end # module Crypto
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
## add convenience "top-level" helpers
|
76
|
+
def sha256( input, engine=nil ) Crypto.sha256( input, engine ); end
|
77
|
+
def sha256hex( input, engine=nil ) Crypto.sha256hex( input, engine ); end
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
|
82
|
+
puts CryptoLite.banner ## say hello
|
@@ -0,0 +1,23 @@
|
|
1
|
+
|
2
|
+
module CryptoLite
|
3
|
+
|
4
|
+
MAJOR = 0
|
5
|
+
MINOR = 0
|
6
|
+
PATCH = 1
|
7
|
+
VERSION = [MAJOR,MINOR,PATCH].join('.')
|
8
|
+
|
9
|
+
def self.version
|
10
|
+
VERSION
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.banner
|
14
|
+
"crypto-lite/#{VERSION} on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}] in (#{root})"
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.root
|
18
|
+
File.expand_path( File.dirname(File.dirname(File.dirname(__FILE__))) )
|
19
|
+
end
|
20
|
+
|
21
|
+
end # module CryptoLite
|
22
|
+
|
23
|
+
|
data/lib/crypto.rb
ADDED
data/lib/crypto/lite.rb
ADDED
data/test/helper.rb
ADDED
data/test/test_hash.rb
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
###
|
2
|
+
# to run use
|
3
|
+
# ruby -I ./lib -I ./test test/test_hash.rb
|
4
|
+
|
5
|
+
|
6
|
+
require 'helper'
|
7
|
+
|
8
|
+
|
9
|
+
class TestHash < MiniTest::Test
|
10
|
+
|
11
|
+
SHA256_EMPTY = 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'
|
12
|
+
SHA256_ABC = 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad'
|
13
|
+
SHA256_A = 'ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb'
|
14
|
+
SHA256_B = '3e23e8160039594a33894f6564e1b1348bbd7a0088d42c4acb73eeaed59c009d'
|
15
|
+
SHA256_C = '2e7d2c03a9507ae265ecf5b5356885a53393a2029d241394997265a1a25aefc6'
|
16
|
+
SHA256_HELLO_CRYPTOS = '33eedea60b0662c66c289ceba71863a864cf84b00e10002ca1069bf58f9362d5'
|
17
|
+
|
18
|
+
|
19
|
+
def test_empty
|
20
|
+
assert_equal SHA256_EMPTY, sha256( '' )
|
21
|
+
assert_equal SHA256_EMPTY, sha256( '', :openssl )
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_abc
|
25
|
+
['abc',
|
26
|
+
'abc'.b,
|
27
|
+
"\x61\x62\x63",
|
28
|
+
0x616263
|
29
|
+
].each do |input|
|
30
|
+
assert_equal SHA256_ABC, sha256( input )
|
31
|
+
assert_equal SHA256_ABC, sha256( input, :openssl )
|
32
|
+
end
|
33
|
+
|
34
|
+
['616263',
|
35
|
+
'0x616263',
|
36
|
+
'0X616263'
|
37
|
+
].each do |input|
|
38
|
+
assert_equal SHA256_ABC, sha256hex( input )
|
39
|
+
end
|
40
|
+
## pp sha256hex( 'hello' ) -- fails - uses non-hex chars
|
41
|
+
|
42
|
+
# a = dec (97), hex (61), bin (01100001)
|
43
|
+
|
44
|
+
[ 'a',
|
45
|
+
"\x61",
|
46
|
+
0b01100001,
|
47
|
+
0x61
|
48
|
+
].each do |input|
|
49
|
+
assert_equal SHA256_A, sha256( input )
|
50
|
+
assert_equal SHA256_A, sha256( input, :openssl )
|
51
|
+
end
|
52
|
+
|
53
|
+
['61',
|
54
|
+
'0x61',
|
55
|
+
'0X61'
|
56
|
+
].each do |input|
|
57
|
+
assert_equal SHA256_A, sha256hex( input )
|
58
|
+
end
|
59
|
+
|
60
|
+
[ 'b',
|
61
|
+
0b01100010
|
62
|
+
].each do |input|
|
63
|
+
assert_equal SHA256_B, sha256( input )
|
64
|
+
end
|
65
|
+
|
66
|
+
[ 'c',
|
67
|
+
0b01100011
|
68
|
+
].each do |input|
|
69
|
+
assert_equal SHA256_C, sha256( input )
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
def test_misc
|
75
|
+
assert_equal SHA256_HELLO_CRYPTOS, sha256( 'Hello, Cryptos!' )
|
76
|
+
end
|
77
|
+
|
78
|
+
end # class TestHash
|
@@ -0,0 +1,19 @@
|
|
1
|
+
###
|
2
|
+
# to run use
|
3
|
+
# ruby -I ./lib -I ./test test/test_version.rb
|
4
|
+
|
5
|
+
|
6
|
+
require 'helper'
|
7
|
+
|
8
|
+
|
9
|
+
class TestVersion < MiniTest::Test
|
10
|
+
|
11
|
+
def test_version
|
12
|
+
pp CryptoLite.version
|
13
|
+
pp CryptoLite.banner
|
14
|
+
pp CryptoLite.root
|
15
|
+
|
16
|
+
assert true ## (for now) everything ok if we get here
|
17
|
+
end
|
18
|
+
|
19
|
+
end # class TestVersion
|
metadata
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: crypto-lite
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Gerald Bauer
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-01-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rdoc
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.0'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '7'
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '4.0'
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '7'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: hoe
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '3.22'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '3.22'
|
47
|
+
description: crypto-lite - Cryptographic Secure Hash Functions and Public Key Signature
|
48
|
+
Algorithms Made Easy
|
49
|
+
email: wwwmake@googlegroups.com
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files:
|
53
|
+
- CHANGELOG.md
|
54
|
+
- Manifest.txt
|
55
|
+
- README.md
|
56
|
+
files:
|
57
|
+
- CHANGELOG.md
|
58
|
+
- Manifest.txt
|
59
|
+
- README.md
|
60
|
+
- Rakefile
|
61
|
+
- lib/crypto-lite.rb
|
62
|
+
- lib/crypto-lite/version.rb
|
63
|
+
- lib/crypto.rb
|
64
|
+
- lib/crypto/lite.rb
|
65
|
+
- test/helper.rb
|
66
|
+
- test/test_hash.rb
|
67
|
+
- test/test_version.rb
|
68
|
+
homepage: https://github.com/rubycoco/blockchain
|
69
|
+
licenses:
|
70
|
+
- Public Domain
|
71
|
+
metadata: {}
|
72
|
+
post_install_message:
|
73
|
+
rdoc_options:
|
74
|
+
- "--main"
|
75
|
+
- README.md
|
76
|
+
require_paths:
|
77
|
+
- lib
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '2.3'
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
requirements: []
|
89
|
+
rubygems_version: 3.1.4
|
90
|
+
signing_key:
|
91
|
+
specification_version: 4
|
92
|
+
summary: crypto-lite - Cryptographic Secure Hash Functions and Public Key Signature
|
93
|
+
Algorithms Made Easy
|
94
|
+
test_files: []
|