class OsslRsa::FileOp

File process class.

Constants

DER_EXTENSION

der extension.

PEM_EXTENSION

pem extension.

PRIVATE_FILE

private file.

PUBLIC_FILE

public file.

Public Class Methods

create_file_name(file_name, add_now=false) click to toggle source

create file name. @param [String] file_name file name. @param [booelan] add_now add now date string flag. @return [String] file name

# File lib/ossl_rsa/file_op.rb, line 96
def self.create_file_name(file_name, add_now=false)

  # if add_now = false, return file_name
  return file_name unless add_now

  # add date string.
  if add_now
    file_name = "#{file_name}_#{DateTime.now.strftime('%Y%m%d%H%M%S')}"
  end
  file_name
end
create_file_path(dir_path, mode, add_now=false) click to toggle source

create save file path. @param [String] dir_path save dir path. absolute. @param [integer] mode pem or der. @param [boolean] add_now add now date string flag. @return [Hash] save file path pair. xxx = private file path, xxx = public file path.

# File lib/ossl_rsa/file_op.rb, line 75
def self.create_file_path(dir_path, mode, add_now=false)

  # create file path.
  private_path = create_file_name(PRIVATE_FILE, add_now)
  public_path = create_file_name(PUBLIC_FILE, add_now)

  file_path_pair = nil
  # add extension.
  if mode == PEM
    file_path_pair = { private: File.join(dir_path, "#{private_path}#{PEM_EXTENSION}"), public: File.join(dir_path, "#{public_path}#{PEM_EXTENSION}")}
  elsif mode == DER
    file_path_pair = { private: File.join(dir_path, "#{private_path}#{DER_EXTENSION}"), public: File.join(dir_path, "#{public_path}#{DER_EXTENSION}")}
  end
      
  file_path_pair
end
get_write_mode(mode) click to toggle source

get file write mode. @param [integer] mode pem or der @return [String] write mode.

# File lib/ossl_rsa/file_op.rb, line 111
def self.get_write_mode(mode)

  # if pem, return w mode.
  return "w" if mode == PEM

  "wb"
end
puts(file_path, write_mode, key_pair) click to toggle source

write to file. @param [String] file_path save file path. @param [String] write_mode file write mode. @param [Hash] key_pair. private and public key pair.

# File lib/ossl_rsa/file_op.rb, line 123
def self.puts(file_path, write_mode, key_pair)

  # write to file.
  File.open(file_path, write_mode) do |file|
    file.puts(key_pair[:private], key_pair[:public])
  end
end
save(dir_path, key_pair, mode, add_now=false) click to toggle source

save file private and public key. @param [String] dir_path save dir path. absolute. @param [Hash] key_pair private and public key pair. @param [integer] mode pem or der. @param [boolean] add_now add now date string flag. @return [Hash] save file path pair. xxx = private file path, xxx = public file path.

# File lib/ossl_rsa/file_op.rb, line 24
def self.save(dir_path, key_pair, mode, add_now=false)

  file_path_pair = create_file_path(dir_path, mode, add_now)

  # save file.
  save_file(key_pair, file_path_pair, mode)
end
save_file(key_pair, file_path_pair, mode) click to toggle source

save file private and public key. @param [Hash] key_pair. private and public key pair. @param [Hash] file_path_pair save file path pair. xxx = private file path, xxx = public file path. @param [integer] mode pem or der. @return [Hash] save file path pair. xxx = private file path, xxx = public file path.

# File lib/ossl_rsa/file_op.rb, line 49
def self.save_file(key_pair, file_path_pair, mode)

  save_path_pair = file_path_pair
  write_mode = get_write_mode(mode)

  # save file.
  unless key_pair[:private].nil?
    write(save_path_pair[:private], write_mode, key_pair[:private])
  else
    save_path_pair[:private] = nil
  end

  unless key_pair[:public].nil?
    write(save_path_pair[:public], write_mode, key_pair[:public])
  else
    save_path_pair[:public] = nil
  end

  save_path_pair
end
save_one_file(file_path, key_pair, mode) click to toggle source

save one file. @param [String] file_path save file path. @param [Hash] key_pair. private and public key pair. @param [integer] mode pem or der.

# File lib/ossl_rsa/file_op.rb, line 36
def self.save_one_file(file_path, key_pair, mode)

  write_mode = get_write_mode(mode)

  # save file.
  puts(file_path, write_mode, key_pair)
end
write(file_path, write_mode, key) click to toggle source

write to file. @param [String] file_path save file path. @param [String] write_mode file write mode @param [String] key save key.

# File lib/ossl_rsa/file_op.rb, line 135
def self.write(file_path, write_mode, key)

  # write to file.
  File.open(file_path, write_mode) do |file|
    file.write(key)
  end
end