ncypher 0.6.2 → 0.7.0

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
- SHA1:
3
- metadata.gz: cfd1b3326757bdf0d4c6e574244fef35590c0e82
4
- data.tar.gz: 256719f1a6f1ae607a368f53f409a1b6c006f4ca
2
+ SHA256:
3
+ metadata.gz: 5ae6bde2a016dfd99b8749cb7e4f323d68ff240bfc89155a514613de302b02d9
4
+ data.tar.gz: 0d5911bb280ee42d10f34050feb5ccfa6d03d2dce8cead6d1912981ed6ca33ac
5
5
  SHA512:
6
- metadata.gz: f75236a2af542fa8846200cc32bcd38e84fde0e72f8be468d41d1b03900b19f85795646dce6934a10e903877a1a86c09691aaf5993f39aadd88b799d7854de1e
7
- data.tar.gz: ae27d9edee23c8ca7f38f69bd9b392f36fa8318fe14ad063a93d5756c5cda0467f9238da36c97fe7e85173223f5c8daecf5c74facdbd008e55593a0d564e3a2a
6
+ metadata.gz: f9b2cf4f291a6f56e36a1bff7a9b3adee281494835f93295092cbccffe2dacecaf656dbf0f9d0ba79a75bd055f232c41e3a51e5386aa8dd9fe768482bc42248f
7
+ data.tar.gz: 847d72ac2a8b21321a18c2eb4b6e49ee98ba6dd8dd7ac554ab25c0681c2ceff66f775064a1e573e11e97f8a12cea0112710b32400d6151786ee59b9c422dff17
data/.travis.yml CHANGED
@@ -3,7 +3,7 @@ cache:
3
3
  bundler: true
4
4
  language: ruby
5
5
  rvm:
6
- - 2.2.1
6
+ - 2.2.6
7
7
  before_install: gem install bundler -v 1.12.5
8
8
  script: bundle exec rake
9
9
  deploy:
data/README.md CHANGED
@@ -68,7 +68,25 @@ defaults: &defaults
68
68
  my_password: <%= Ncypher::Ncypher.decrypt('lXEwfKv4dEjmK0kojEAnikNsLCsVCtSMiR2aSfM6uUXYn2DzCZ3O7SA9HaGnMp/kEEsI') %>
69
69
  ```
70
70
 
71
+ ## Password derived secret key
71
72
 
73
+ In some cases you may want to derive a key from a particular password you have memorized. You can simply do:
74
+
75
+ ```
76
+ $> ncypher derive_key p4$$w0rd
77
+ R9RgHcFnuHr+86/7v3MdDyu3V63jh69VCPMXknA2v6E=
78
+ SALT: 4+d4JTGTxRbtXs1vYScBYg==
79
+ ```
80
+
81
+ You can see that the salt is randomly generated for security reasons. You should put that salt in a `.ncypher_salt` file in the current directory (this file can be pushed to your repository). So that the next time you do `ncypher derive_key p4$$w0rd` you get the exact same ncyper\_key generated.
82
+ Note that the salt is written on STDERR so you can directly do:
83
+
84
+ ```
85
+ $> ncypher derive_key p4$$w0rd > .ncypher_key
86
+ SALT: WKCAkJcS65nx3lA/w1BmBw==
87
+ ```
88
+
89
+ Then you have the ncypher\_key in .ncypher\_key. Be sure to save the salt if you want to be able to derive back the exact same key in the future.
72
90
 
73
91
 
74
92
  ## Development
data/exe/ncypher CHANGED
@@ -1,10 +1,10 @@
1
1
  #!/usr/bin/env ruby
2
- require 'ncypher'
2
+ require "ncypher"
3
3
 
4
4
  begin
5
- Object.const_get('Ncypher')
5
+ Object.const_get("Ncypher")
6
6
  rescue NameError
7
- require 'bundler/setup'
7
+ require "bundler/setup"
8
8
  end
9
9
 
10
10
  SUB_COMMANDS = %w(generate_key encrypt decrypt)
@@ -12,6 +12,7 @@ SUB_COMMANDS = %w(generate_key encrypt decrypt)
12
12
  if ARGV.empty?
13
13
  STDERR.puts "Ncypher a credential encryption tool"
14
14
  STDERR.puts "usage: ncypher generate_key"
15
+ STDERR.puts "usage: ncypher derive_key <password> [salt]"
15
16
  STDERR.puts "usage: ncypher key"
16
17
  STDERR.puts "usage: ncypher encrypt <text>"
17
18
  STDERR.puts "usage: ncypher decrypt <text>"
@@ -20,14 +21,24 @@ end
20
21
 
21
22
  cmd = ARGV.shift
22
23
  case cmd
23
- when "generate_key"
24
- puts Ncypher::Ncypher.new.generate_key
25
- when "key"
26
- puts Ncypher::Ncypher.new.key_b64
27
- when "encrypt"
28
- text = (ARGV.shift || STDIN.read)
29
- puts Ncypher::Ncypher.new.encrypt(text.strip)
30
- when "decrypt"
31
- text = (ARGV.shift || STDIN.read)
32
- puts Ncypher::Ncypher.new.decrypt(text.strip)
24
+ when "generate_key"
25
+ puts Ncypher::Ncypher.new.generate_key
26
+ when "derive_key"
27
+ password = ARGV.shift
28
+ unless password
29
+ abort "ncypher derive_key <password> [salt]"
30
+ end
31
+ salt = File.exists?(".ncypher_salt") ? File.read(".ncypher_salt")&.strip : ARGV.shift
32
+ key, used_salt = Ncypher::Ncypher.new.derive_key(password.strip, salt)
33
+ STDOUT.puts key
34
+ STDERR.puts "SALT: #{used_salt}" # Put salt on stderr so we can do ncypher deriver_key password > .ncypher_key
35
+ # and keep salt out of .ncypher_key
36
+ when "key"
37
+ puts Ncypher::Ncypher.new.key_b64
38
+ when "encrypt"
39
+ text = (ARGV.shift || STDIN.read)
40
+ puts Ncypher::Ncypher.new.encrypt(text.strip)
41
+ when "decrypt"
42
+ text = (ARGV.shift || STDIN.read)
43
+ puts Ncypher::Ncypher.new.decrypt(text.strip)
33
44
  end
data/lib/ncypher.rb CHANGED
@@ -1,14 +1,14 @@
1
1
  require "ncypher/version"
2
2
 
3
- require 'base64'
4
- require 'rbnacl/libsodium'
5
- require 'rbnacl'
3
+ require "base64"
4
+ require "rbnacl/libsodium"
5
+ require "rbnacl/password_hash"
6
6
 
7
- module Ncypher
7
+ require "rbnacl"
8
8
 
9
+ module Ncypher
9
10
  class Ncypher
10
-
11
- def initialize(key_filename: '.ncypher_key', key: nil)
11
+ def initialize(key_filename: ".ncypher_key", key: nil)
12
12
  @key = key ? Base64.strict_decode64(key.strip) : nil
13
13
  @key_filename = key_filename
14
14
  end
@@ -26,12 +26,24 @@ module Ncypher
26
26
  Base64.strict_encode64(generated_key)
27
27
  end
28
28
 
29
+ def derive_key(password, encoded_salt = nil)
30
+ salt ||= encoded_salt ?
31
+ Base64.strict_decode64(encoded_salt) :
32
+ RbNaCl::Random.random_bytes(RbNaCl::PasswordHash::Argon2::SALTBYTES)
33
+
34
+ opslimit = 5
35
+ memlimit = 7_256_678
36
+ digest_size = RbNaCl::SecretBox.key_bytes
37
+ generated_key = RbNaCl::PasswordHash.argon2(password, salt, opslimit, memlimit, digest_size)
38
+ [Base64.strict_encode64(generated_key), Base64.strict_encode64(salt)]
39
+ end
40
+
29
41
  def key
30
42
  @key ||= begin
31
- saved_key = ENV['NCYPHER_KEY'] || find_keyfile
32
- abort "Can't find .ncypher_key file or NCYPHER_KEY env variable" if saved_key.nil?
33
- Base64.strict_decode64(saved_key.strip)
34
- end
43
+ saved_key = ENV["NCYPHER_KEY"] || find_keyfile
44
+ abort "Can't find .ncypher_key file or NCYPHER_KEY env variable" if saved_key.nil?
45
+ Base64.strict_decode64(saved_key.strip)
46
+ end
35
47
  end
36
48
 
37
49
  def key_b64
@@ -50,6 +62,10 @@ module Ncypher
50
62
  Ncypher.new.generate_key
51
63
  end
52
64
 
65
+ def self.derive_key(password, salt = nil)
66
+ Ncypher.new.derive_key(password, salt)
67
+ end
68
+
53
69
  def self.key
54
70
  Ncypher.new.key
55
71
  end
@@ -59,18 +75,17 @@ module Ncypher
59
75
  end
60
76
 
61
77
  private
78
+
62
79
  def box
63
80
  RbNaCl::SimpleBox.from_secret_key(key)
64
81
  end
65
82
 
66
- def find_keyfile(folder: '.')
83
+ def find_keyfile(folder: ".")
67
84
  path = "#{folder}/#{@key_filename}"
68
85
  return File.read(path) if File.exist?(path)
69
- return nil if folder == '/'
86
+ return nil if folder == "/"
70
87
  folder = File.expand_path("#{folder}/../")
71
88
  find_keyfile(folder: folder)
72
89
  end
73
-
74
90
  end
75
-
76
91
  end
@@ -1,3 +1,3 @@
1
1
  module Ncypher
2
- VERSION = "0.6.2"
2
+ VERSION = "0.7.0"
3
3
  end
data/ncypher.gemspec CHANGED
@@ -24,5 +24,5 @@ Gem::Specification.new do |spec|
24
24
  spec.add_development_dependency "minitest", "~> 5.0"
25
25
 
26
26
  spec.add_dependency 'rbnacl-libsodium', '~> 1.0'
27
- spec.add_dependency 'rbnacl', '~> 3.0'
27
+ spec.add_dependency 'rbnacl', '~> 5.0'
28
28
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ncypher
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Hagege
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-09-05 00:00:00.000000000 Z
11
+ date: 2019-09-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -72,14 +72,14 @@ dependencies:
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '3.0'
75
+ version: '5.0'
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '3.0'
82
+ version: '5.0'
83
83
  description: ''
84
84
  email:
85
85
  - david.hagege@gmail.com
@@ -121,7 +121,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
121
  version: '0'
122
122
  requirements: []
123
123
  rubyforge_project:
124
- rubygems_version: 2.6.13
124
+ rubygems_version: 2.7.7
125
125
  signing_key:
126
126
  specification_version: 4
127
127
  summary: Ncypher lets you encrypt/decrypt credentials in a safe and transparent way