ltec 0.1.1
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/Gemfile +8 -0
- data/Gemfile.lock +25 -0
- data/README.md +38 -0
- data/Rakefile +4 -0
- data/exe/ltectool +42 -0
- data/lib/ltec/version.rb +5 -0
- data/lib/ltec.rb +114 -0
- data/ltec.gemspec +43 -0
- data/sig/ltec.rbs +4 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f8d26733395b3b78dc2ec834297af2fe7773acf8f4f3b9de6681f226b4d82fa3
|
4
|
+
data.tar.gz: 1f37579b78dde51aa930d1cac9511837ee399dfd3924344e9f2ebff4f0032065
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ff58f4e4698ca7f495627db0763cba642dbe60abd32583feb653dbbdc14e0cfa447c4d27ffc56977331cfe24098a5eec33cdd6f4b1f2a8d6df99551b583c2751
|
7
|
+
data.tar.gz: 9d3b5eb65fd5bab11c43dafda2e83aa6586c0fa9038e5b05c79af41e9df5d1856b7702b50ad0f3f31c214c95452090e42774ea0052f46540f3c06ea842c6bd8b
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
ltec (0.1.0)
|
5
|
+
base64 (~> 0.1.0)
|
6
|
+
openssl (~> 2.2.0)
|
7
|
+
salsa20 (~> 0.1.3)
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: https://rubygems.org/
|
11
|
+
specs:
|
12
|
+
base64 (0.1.0)
|
13
|
+
openssl (2.2.0)
|
14
|
+
rake (13.0.3)
|
15
|
+
salsa20 (0.1.3)
|
16
|
+
|
17
|
+
PLATFORMS
|
18
|
+
x86_64-darwin-20
|
19
|
+
|
20
|
+
DEPENDENCIES
|
21
|
+
ltec!
|
22
|
+
rake (~> 13.0)
|
23
|
+
|
24
|
+
BUNDLED WITH
|
25
|
+
2.3.13
|
data/README.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# Ltec
|
2
|
+
|
3
|
+
a tinny tool to encryt your private message via ECC(Secp256k1)
|
4
|
+
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Install the gem and add to the application's Gemfile by executing:
|
9
|
+
|
10
|
+
$ bundle add ltec
|
11
|
+
|
12
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
13
|
+
|
14
|
+
$ gem install ltec
|
15
|
+
|
16
|
+
## Usage
|
17
|
+
|
18
|
+
``` ruby
|
19
|
+
require 'ltec'
|
20
|
+
kp = Ltec::EC.generateKeyPair()
|
21
|
+
privateKeyString = kp['seckey']
|
22
|
+
publicKeyString = kp['pubkey']
|
23
|
+
|
24
|
+
message = "hello ruby"
|
25
|
+
encMsg = Ltec::EC.encrypt(publicKeyString,msg)
|
26
|
+
decryptMsg = Ltec::EC.decrypt(privateKeyString,msg)
|
27
|
+
|
28
|
+
```
|
29
|
+
|
30
|
+
## Development
|
31
|
+
|
32
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
33
|
+
|
34
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
35
|
+
|
36
|
+
## Contributing
|
37
|
+
|
38
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ltec.
|
data/Rakefile
ADDED
data/exe/ltectool
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "ltec"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require "ltec"
|
15
|
+
cmd = ARGV[0]
|
16
|
+
|
17
|
+
if cmd == 'g'
|
18
|
+
kp = Ltec::EC.generateKeyPair()
|
19
|
+
puts "publickey: #{kp['pubkey']}"
|
20
|
+
puts "privatekey: #{kp['seckey']}"
|
21
|
+
elsif cmd == 'e'
|
22
|
+
pubkey = ARGV[1]
|
23
|
+
msg = ARGV[2]
|
24
|
+
enc = Ltec::EC.encrypt(pubkey,msg)
|
25
|
+
puts enc
|
26
|
+
elsif cmd == 'd'
|
27
|
+
prikey = ARGV[1]
|
28
|
+
enc = ARGV[2]
|
29
|
+
msg = Ltec::EC.decrypt(prikey,enc)
|
30
|
+
puts msg
|
31
|
+
|
32
|
+
else
|
33
|
+
puts <<EOF
|
34
|
+
ltectool cmd [options]
|
35
|
+
commands
|
36
|
+
g generate key pair
|
37
|
+
e publickey message
|
38
|
+
d privatekey message
|
39
|
+
|
40
|
+
EOF
|
41
|
+
end
|
42
|
+
|
data/lib/ltec/version.rb
ADDED
data/lib/ltec.rb
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "ltec/version"
|
4
|
+
require 'openssl'
|
5
|
+
require "base64"
|
6
|
+
require 'salsa20'
|
7
|
+
|
8
|
+
module Ltec
|
9
|
+
class Error < StandardError; end
|
10
|
+
# Your code goes here...
|
11
|
+
class EC
|
12
|
+
SECP256K1 = 'secp256k1'
|
13
|
+
def EC.base64(str)
|
14
|
+
return Base64.strict_encode64(str)
|
15
|
+
end
|
16
|
+
def EC.base64Decode(str)
|
17
|
+
return Base64.decode64(str)
|
18
|
+
end
|
19
|
+
def EC.toHex(str)
|
20
|
+
return str.unpack('H*')[0]
|
21
|
+
end
|
22
|
+
def EC.fromHex(hex)
|
23
|
+
return [hex].pack("H*")
|
24
|
+
end
|
25
|
+
def EC.hexToBase64(hex)
|
26
|
+
return [[hex].pack("H*")].pack("m")
|
27
|
+
end
|
28
|
+
def EC.base64ToHex(base64)
|
29
|
+
return base64.unpack("m*")[0].unpack('H*')[0]
|
30
|
+
end
|
31
|
+
|
32
|
+
def EC.generateKeyPair()
|
33
|
+
ec1 = OpenSSL::PKey::EC.generate(SECP256K1)
|
34
|
+
seckey = EC.hexToBase64(ec1.private_key.to_s(16))
|
35
|
+
pubkey = EC.hexToBase64(ec1.public_key.to_bn(:compressed).to_s(16))
|
36
|
+
return {"seckey" => seckey.strip,"pubkey" => pubkey.strip}
|
37
|
+
end
|
38
|
+
|
39
|
+
def EC.encrypt(pubKey,msg)
|
40
|
+
hex = base64ToHex(pubKey)
|
41
|
+
ec = OpenSSL::PKey::EC.new(SECP256K1)
|
42
|
+
# puts ec
|
43
|
+
# puts ec.group
|
44
|
+
pubNum = OpenSSL::BN.new(hex,16)
|
45
|
+
pt = OpenSSL::PKey::EC::Point.new(ec.group,pubNum)
|
46
|
+
|
47
|
+
randKey = OpenSSL::Random.random_bytes(32)
|
48
|
+
hexRnd = toHex(randKey)
|
49
|
+
rndBn = OpenSSL::BN.new(hexRnd,16)
|
50
|
+
|
51
|
+
# 0 < rndBn < OrderOfG
|
52
|
+
if rndBn.zero?
|
53
|
+
raise "OpenSSL::Random.random_bytes generate Fail"
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
rndPt = pt.mul(0,rndBn)
|
58
|
+
hexpt = rndPt.to_bn(:compressed).to_s(16)
|
59
|
+
|
60
|
+
empherPub = fromHex(hexpt)
|
61
|
+
|
62
|
+
#ecdh
|
63
|
+
ptDh = pt.mul(rndBn)
|
64
|
+
ptX = fromHex(ptDh.to_bn(:compressed).to_s(16))[1...33]
|
65
|
+
|
66
|
+
dhHash = OpenSSL::Digest.digest("SHA512", ptX)
|
67
|
+
|
68
|
+
nonce = OpenSSL::Random.random_bytes(8)
|
69
|
+
encryptor = Salsa20.new(dhHash[0...32], nonce)
|
70
|
+
encrypted_text = encryptor.encrypt(msg)
|
71
|
+
|
72
|
+
dataforMac = nonce + empherPub + encrypted_text
|
73
|
+
mac = OpenSSL::HMAC.digest('sha256', dhHash[32,64], dataforMac)
|
74
|
+
|
75
|
+
#
|
76
|
+
return base64(fromHex('0300080020002100') + nonce + mac + empherPub + encrypted_text)
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
def EC.decrypt(secKey,base64Cipher)
|
81
|
+
encResult = base64Decode(base64Cipher)
|
82
|
+
nonce = encResult[8...16]
|
83
|
+
mac = encResult[16...48]
|
84
|
+
tmpPub = encResult[48...81]
|
85
|
+
dataEnc = encResult[81...encResult.length]
|
86
|
+
|
87
|
+
tmpPubHex = toHex(tmpPub)
|
88
|
+
ec = OpenSSL::PKey::EC.new(SECP256K1)
|
89
|
+
tmpBn = OpenSSL::BN.new(tmpPubHex,16)
|
90
|
+
|
91
|
+
tmpPt = OpenSSL::PKey::EC::Point.new(ec.group,tmpBn)
|
92
|
+
|
93
|
+
priKey = OpenSSL::BN.new(toHex(base64Decode(secKey)),16)
|
94
|
+
ptDh = tmpPt.mul(priKey);
|
95
|
+
|
96
|
+
ptX = fromHex(ptDh.to_bn(:compressed).to_s(16))[1...33]
|
97
|
+
dhHash = OpenSSL::Digest.digest("SHA512", ptX)
|
98
|
+
|
99
|
+
key = dhHash[0...32]
|
100
|
+
hmakkey = dhHash[32...64]
|
101
|
+
|
102
|
+
# check mac
|
103
|
+
dataforMac = nonce + tmpPub + dataEnc
|
104
|
+
mac2 = OpenSSL::HMAC.digest('sha256', hmakkey, dataforMac)
|
105
|
+
if mac2 != mac
|
106
|
+
raise 'Mac not Fit,the privateKey is not fit'
|
107
|
+
end
|
108
|
+
|
109
|
+
encryptor = Salsa20.new(key, nonce)
|
110
|
+
txt = encryptor.decrypt(dataEnc)
|
111
|
+
return txt
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
data/ltec.gemspec
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/ltec/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "ltec"
|
7
|
+
spec.version = Ltec::VERSION
|
8
|
+
spec.authors = ["vitock"]
|
9
|
+
spec.email = [""]
|
10
|
+
|
11
|
+
spec.summary = "using ECC to enrypt private message"
|
12
|
+
spec.description = "using ECC to enrypt private message with ruby ,the curve is SECP256k1 "
|
13
|
+
spec.homepage = "https://github.com/vitock/ltec_rb.git"
|
14
|
+
spec.required_ruby_version = ">= 2.6.0"
|
15
|
+
|
16
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
|
17
|
+
|
18
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
19
|
+
spec.metadata["source_code_uri"] = "https://github.com/vitock/ltec_rb.git"
|
20
|
+
spec.metadata["changelog_uri"] = "https://github.com/vitock/ltec_rb/blob/master/README.md"
|
21
|
+
|
22
|
+
# Specify which files should be added to the gem when it is released.
|
23
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
24
|
+
spec.files = Dir.chdir(__dir__) do
|
25
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
26
|
+
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
|
27
|
+
end
|
28
|
+
end
|
29
|
+
spec.bindir = "exe"
|
30
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
31
|
+
spec.require_paths = ["lib"]
|
32
|
+
|
33
|
+
# Uncomment to register a new dependency of your gem
|
34
|
+
# spec.add_dependency "example-gem", "~> 1.0"
|
35
|
+
|
36
|
+
spec.add_dependency "openssl", "~> 2.2.0"
|
37
|
+
spec.add_dependency "base64", "~> 0.1.0"
|
38
|
+
spec.add_dependency "salsa20", "~> 0.1.3"
|
39
|
+
|
40
|
+
|
41
|
+
# For more information and examples about making a new gem, check out our
|
42
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
43
|
+
end
|
data/sig/ltec.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ltec
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- vitock
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-05-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: openssl
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 2.2.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 2.2.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: base64
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.1.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.1.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: salsa20
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.1.3
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.1.3
|
55
|
+
description: 'using ECC to enrypt private message with ruby ,the curve is SECP256k1 '
|
56
|
+
email:
|
57
|
+
- ''
|
58
|
+
executables:
|
59
|
+
- ltectool
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- Gemfile
|
64
|
+
- Gemfile.lock
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- exe/ltectool
|
68
|
+
- lib/ltec.rb
|
69
|
+
- lib/ltec/version.rb
|
70
|
+
- ltec.gemspec
|
71
|
+
- sig/ltec.rbs
|
72
|
+
homepage: https://github.com/vitock/ltec_rb.git
|
73
|
+
licenses: []
|
74
|
+
metadata:
|
75
|
+
homepage_uri: https://github.com/vitock/ltec_rb.git
|
76
|
+
source_code_uri: https://github.com/vitock/ltec_rb.git
|
77
|
+
changelog_uri: https://github.com/vitock/ltec_rb/blob/master/README.md
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: 2.6.0
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubygems_version: 3.2.15
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: using ECC to enrypt private message
|
97
|
+
test_files: []
|