beowulf-ruby 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d80b10af00162deb40c9d71f723e81a093de9404d02fb92e53388942a8d5091b
4
- data.tar.gz: 281e7fbe7473dc501b5197fcb7e3a4c1400fab14899a55d1e5c56b7b1f7cb94a
3
+ metadata.gz: 3def3bec1684fb6953155498d1bd5b8d8264e72fb4b1ca810a2793e75abce77c
4
+ data.tar.gz: 582fcc0e8cfc471f2b4ed4a53286414b7b6e41784e27c662d052e35c3463b6d6
5
5
  SHA512:
6
- metadata.gz: 62467694a5be6efa7831f0d951cbecba2ba44186a7fe1936e8b5177b609eacd266fd4769b335d4091b489a4c9b479c20bf050020e942373d69aaa49846a1f8a5
7
- data.tar.gz: f20991a1c2c2604bd8061d51c2ab8030b3b11dad2005397d7f388a2b6c945eed19d9ea120dae9d6e2c96a018e88fbd154138848f065b0a2f7404a28501a324fc
6
+ metadata.gz: 679b0a017ae55f9a0eec13725bd9e0097b5e5d6500db8082fb57d5c348a05a971a3704afb1ee9cc4c8c69e1a325e20d68f6873b3af3cbf47d262dc7427242a4e
7
+ data.tar.gz: 11b8a8530afbfd05d7e6833a07b518f5b93bab688f39d9b71e7cd0f57a1aad38d0a084dafdb86f29db68d3068bbb71bab272b55847657f1dde2c0f233739258b
@@ -34,7 +34,7 @@ module Beowulf
34
34
  bytes
35
35
  end
36
36
 
37
- def to_json(options)
37
+ def to_json(options = {})
38
38
  JSON.dump ({
39
39
  :weight_threshold => @weight_threshold,
40
40
  :account_auths => @account_auths,
@@ -35,7 +35,7 @@ module Beowulf
35
35
  bytes
36
36
  end
37
37
 
38
- def to_json(options)
38
+ def to_json(options = {})
39
39
  JSON.dump ({
40
40
  :weight_threshold => @weight_threshold,
41
41
  :account_auths => @account_auths,
@@ -1,4 +1,4 @@
1
1
  module Beowulf
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  AGENT_ID = "beowulf/#{VERSION}"
4
4
  end
@@ -8,6 +8,8 @@ module Beowulf
8
8
  include ChainConfig
9
9
  include Utils
10
10
 
11
+ attr_accessor :name, :private_key, :public_key, :cipher_keys, :cipher_type, :salt
12
+
11
13
  def initialize(options = {})
12
14
  @name = options[:name] || ''
13
15
  @private_key = options[:private_key] || ''
@@ -17,16 +19,15 @@ module Beowulf
17
19
  @salt = options[:salt] || ''
18
20
  end
19
21
 
20
- def name
21
- @name
22
- end
23
-
24
- def private_key
25
- @private_key
26
- end
27
-
28
- def public_key
29
- @public_key
22
+ def to_json(options = {})
23
+ JSON.dump ({
24
+ :name => @name,
25
+ :private_key => @private_key,
26
+ :public_key => @public_key,
27
+ :cipher_keys => @cipher_keys,
28
+ :cipher_type => @cipher_type,
29
+ :salt => @salt
30
+ })
30
31
  end
31
32
 
32
33
  def gen_keys
@@ -126,6 +127,36 @@ module Beowulf
126
127
  File.open(file_path, "w") { |file| file.puts wallet_json}
127
128
  end
128
129
 
130
+ def encode_wallet(password)
131
+ # Validate
132
+ if @name == nil || @name.length == 0
133
+ puts "name is not empty."
134
+ raise WalletError, "name is not empty."
135
+ end
136
+
137
+ # Encrypt data
138
+ # Create checksum
139
+ @salt = SecureRandom.alphanumeric(16)
140
+ new_password = password + @salt
141
+ pw_bytes = pakStr(new_password)
142
+ checksum = Digest::SHA512.digest(pw_bytes)
143
+ chs = hexlify(checksum)
144
+ # Create plain_data
145
+ keys = {@public_key => @private_key}
146
+ plain_keys = {"checksum" => chs, "keys" => keys}
147
+ plain_data = JSON.dump(plain_keys)
148
+
149
+ # Encrypt AES
150
+ msg_encrypt = encrypt(chs, plain_data)
151
+ msg_hex = hexlify(msg_encrypt)
152
+ @cipher_keys = msg_hex
153
+
154
+ # Create data save.
155
+ wallet_data = {"cipher_keys" => @cipher_keys, "cipher_type" => @cipher_type, "salt" => @salt, "name" => @name}
156
+ wallet_json = JSON.dump(wallet_data)
157
+ wallet_json
158
+ end
159
+
129
160
  # https://gist.github.com/andrekandore/3140554
130
161
  def encrypt(key, data)
131
162
  block = key[0..31]
@@ -177,6 +208,39 @@ module Beowulf
177
208
  end
178
209
  end
179
210
 
211
+ def decode_wallet(wallet_json_string, password)
212
+ # Validate inputs
213
+ if wallet_json_string == nil || wallet_json_string.length == 0
214
+ puts "Wallet json is not empty."
215
+ raise WalletError, "Wallet json is not empty."
216
+ end
217
+ wallet_json = JSON.parse(wallet_json_string)
218
+ # Init properties wallet
219
+ @name = wallet_json['name']
220
+ @cipher_keys = wallet_json['cipher_keys']
221
+ @salt = wallet_json['salt']
222
+ # Create checksum
223
+ new_password = password + @salt
224
+ pw_bytes = pakStr(new_password)
225
+ checksum = Digest::SHA512.digest(pw_bytes)
226
+ chs = hexlify(checksum)
227
+ # Decrypt data
228
+ data_byte = unhexlify(@cipher_keys)
229
+ # Decrypt AES
230
+ msg_decrypt = decrypt(chs, data_byte)
231
+ msg_json = JSON.parse(msg_decrypt)
232
+ dechs = msg_json['checksum']
233
+ if !(chs.eql? dechs)
234
+ puts "Password wrong."
235
+ raise WalletError, "Password wrong."
236
+ end
237
+ keys = msg_json['keys']
238
+ keys.each do |key, value|
239
+ @public_key = key
240
+ @private_key = value
241
+ end
242
+ end
243
+
180
244
  # https://gist.github.com/andrekandore/3140554
181
245
  def decrypt(key, data)
182
246
  block = key[0..31]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: beowulf-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - NghiaTC
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-02-11 00:00:00.000000000 Z
11
+ date: 2020-02-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler