beowulf-ruby 0.0.2 → 0.0.3
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 +4 -4
- data/lib/beowulf/type/authority.rb +1 -1
- data/lib/beowulf/type/authority_update.rb +1 -1
- data/lib/beowulf/version.rb +1 -1
- data/lib/beowulf/wallet.rb +74 -10
- metadata +2 -2
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA256:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 3def3bec1684fb6953155498d1bd5b8d8264e72fb4b1ca810a2793e75abce77c
         | 
| 4 | 
            +
              data.tar.gz: 582fcc0e8cfc471f2b4ed4a53286414b7b6e41784e27c662d052e35c3463b6d6
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 679b0a017ae55f9a0eec13725bd9e0097b5e5d6500db8082fb57d5c348a05a971a3704afb1ee9cc4c8c69e1a325e20d68f6873b3af3cbf47d262dc7427242a4e
         | 
| 7 | 
            +
              data.tar.gz: 11b8a8530afbfd05d7e6833a07b518f5b93bab688f39d9b71e7cd0f57a1aad38d0a084dafdb86f29db68d3068bbb71bab272b55847657f1dde2c0f233739258b
         | 
    
        data/lib/beowulf/version.rb
    CHANGED
    
    
    
        data/lib/beowulf/wallet.rb
    CHANGED
    
    | @@ -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  | 
| 21 | 
            -
                   | 
| 22 | 
            -
             | 
| 23 | 
            -
             | 
| 24 | 
            -
             | 
| 25 | 
            -
             | 
| 26 | 
            -
             | 
| 27 | 
            -
             | 
| 28 | 
            -
             | 
| 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. | 
| 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 | 
            +
            date: 2020-02-19 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: bundler
         |