ossl_rsa 0.1.0 → 0.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3ea603bcf1f6ccd4ce66c1bac0791dc568953a38
4
- data.tar.gz: a0904b70de93fb4397138a7bc746c481bba154a8
3
+ metadata.gz: 3837b5a7b4215e642b02c31dcd47161c9e0c27b6
4
+ data.tar.gz: 5bf7b2be6864fb700f81301b97a6f07060dd893c
5
5
  SHA512:
6
- metadata.gz: ad76ee44d12a44ba7a6df406042a7d2a7fc66da1ba57c8c9be7c3cc41416a0aadd2325a4cb4874f32cc11ef27793ad18ca0b03177df85294cc593243e8cd4e3e
7
- data.tar.gz: 86e96223cbe904cff5bfa86f738dfd92a1cd3c454e748adf4e36d04742f57e060f51df64ab554d3c2488280a716d054c01000871daea50b19e8218fd54836d90
6
+ metadata.gz: d6d20e3f2bf9fa55a0ff4de7dbfc5ea429e4eccfc5432a658dd3ac819b5a26d892ad51d2f330ce8cbdd5af63dc297389b29acf5ac9f0f354417330aa50956fd1
7
+ data.tar.gz: 470b51541155dbd4753dc89ea71bd27c3fcb2ed3cc481a77c96d1bf18c6e0b056345ce3ed24d62fe1371bc7a56f3b814673f80e4aa1bbda2b3ef1e2dec7fca7b
@@ -0,0 +1,106 @@
1
+ require 'date'
2
+
3
+ # openssl rsa module.
4
+ module OsslRsa
5
+
6
+ # File process class.
7
+ class FileOp
8
+
9
+ # private file.
10
+ PRIVATE_FILE = "private"
11
+ # public file.
12
+ PUBLIC_FILE = "public"
13
+ # pem extension.
14
+ PEM_EXTENSION = ".pem"
15
+ # der extension.
16
+ DER_EXTENSION = ".der"
17
+
18
+ # save file private and public key.
19
+ # @param [String] dir_path save dir path. absolute.
20
+ # @param [Hash] key_pair. private and public key pair.
21
+ # @param [integer] mode pem or der.
22
+ # @param [boolean] add_now add now date string flag.
23
+ # @return [Hash] save file path pair. xxx[:private] = private file path, xxx[:public] = public file path.
24
+ def self.save(dir_path, key_pair, mode, add_now=false)
25
+
26
+ file_path_pair = create_file_path(dir_path, mode, add_now)
27
+ write_mode = get_write_mode(mode)
28
+
29
+ # save file.
30
+ unless key_pair[:private].nil?
31
+ write(file_path_pair[:private], write_mode, key_pair[:private])
32
+ else
33
+ file_path_pair[:private] = nil
34
+ end
35
+
36
+ unless key_pair[:public].nil?
37
+ write(file_path_pair[:public], write_mode, key_pair[:public])
38
+ else
39
+ file_path_pair[:public] = nil
40
+ end
41
+
42
+ file_path_pair
43
+ end
44
+
45
+ # create save file path.
46
+ # @param [String] dir_path save dir path. absolute.
47
+ # @param [integer] mode pem or der.
48
+ # @param [boolean] add_now add now date string flag.
49
+ # @return [Hash] save file path pair. xxx[:private] = private file path, xxx[:public] = public file path.
50
+ def self.create_file_path(dir_path, mode, add_now=false)
51
+
52
+ # create file path.
53
+ private_path = create_file_name(PRIVATE_FILE, add_now)
54
+ public_path = create_file_name(PUBLIC_FILE, add_now)
55
+
56
+ file_path_pair = nil
57
+ # add extension.
58
+ if mode == PEM
59
+ file_path_pair = { private: File.join(dir_path, "#{private_path}#{PEM_EXTENSION}"), public: File.join(dir_path, "#{public_path}#{PEM_EXTENSION}")}
60
+ elsif mode == DER
61
+ file_path_pair = { private: File.join(dir_path, "#{private_path}#{DER_EXTENSION}"), public: File.join(dir_path, "#{public_path}#{DER_EXTENSION}")}
62
+ end
63
+
64
+ file_path_pair
65
+ end
66
+
67
+ # create file name.
68
+ # @param [String] file_name file name.
69
+ # @param [booelan] add_now add now date string flag.
70
+ # @return [String] file name
71
+ def self.create_file_name(file_name, add_now=false)
72
+
73
+ # if add_now = false, return file_name
74
+ return file_name unless add_now
75
+
76
+ # add date string.
77
+ if add_now
78
+ file_name = "#{file_name}_#{DateTime.now.strftime('%Y%m%d%H%M%S')}"
79
+ end
80
+ file_name
81
+ end
82
+
83
+ # get file write mode.
84
+ # @param [String] mode pem or der
85
+ # @return [String] write mode.
86
+ def self.get_write_mode(mode)
87
+
88
+ # if pem, return w mode.
89
+ return "w" if mode == PEM
90
+
91
+ "wb"
92
+ end
93
+
94
+ # write to file.
95
+ # @param [String] file_path save file path.
96
+ # @param [String] write_mode file write mode
97
+ # @param [String] key save key.
98
+ def self.write(file_path, write_mode, key)
99
+
100
+ # write to file.
101
+ File.open(file_path, write_mode) do |file|
102
+ file.write(key)
103
+ end
104
+ end
105
+ end
106
+ end
data/lib/ossl_rsa/rsa.rb CHANGED
@@ -110,6 +110,23 @@ module OsslRsa
110
110
  @rsa.verify(digest, sign, value)
111
111
  end
112
112
 
113
+ # save file key.
114
+ # filename is [private.xxx(pem or der)], [public.xxx(pem or der)]
115
+ # @param [String] dir_path save dir path. absolute.
116
+ # @param [integer] mode pem or der.
117
+ # @param [OpenSSL::Cipher] cipher cipher instance.
118
+ # @param [String] pass password
119
+ # @param [boolean] add_now add now date string flag.
120
+ # @return [Hash] save file path pair. xxx[:private] = private file path, xxx[:public] = public file path.
121
+ def to_file(dir_path, mode, cipher=nil, pass=nil, add_now=false)
122
+
123
+ save_key_pair = key_pair(mode, cipher, pass)
124
+
125
+ # save file.
126
+ file_path_pair = OsslRsa::FileOp.save(dir_path, save_key_pair, mode, add_now)
127
+ file_path_pair
128
+ end
129
+
113
130
  # private check.
114
131
  # @return [boolean] OpsnSSL::PKey::RSA.private?
115
132
  def private?
@@ -124,6 +141,9 @@ module OsslRsa
124
141
 
125
142
  # get private and public key.
126
143
  # private key set self OpenSSL::PKey::RSA instance.
144
+ # @param [integer] mode pem or der.
145
+ # @param [OpenSSL::Cipher] cipher cipher instance.
146
+ # @param [String] pass password
127
147
  # @return [Hash] key pair hash. xx[:private] = private_key, xx[:public] = public_key
128
148
  def key_pair(mode, cipher=nil, pass=nil)
129
149
 
@@ -1,3 +1,3 @@
1
1
  module OsslRsa
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/ossl_rsa.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "ossl_rsa/version"
2
2
  require "ossl_rsa/rsa"
3
+ require "ossl_rsa/file_op"
3
4
 
4
5
  # openssl rsa module.
5
6
  module OsslRsa
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ossl_rsa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - h.shigemoto
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-07-03 00:00:00.000000000 Z
11
+ date: 2016-07-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -70,6 +70,7 @@ files:
70
70
  - bin/console
71
71
  - bin/setup
72
72
  - lib/ossl_rsa.rb
73
+ - lib/ossl_rsa/file_op.rb
73
74
  - lib/ossl_rsa/rsa.rb
74
75
  - lib/ossl_rsa/version.rb
75
76
  - ossl_rsa.gemspec