Class: Code::Object::Cryptography

Inherits:
Code::Object show all
Defined in:
lib/code/object/cryptography.rb

Constant Summary collapse

CLASS_DOCUMENTATION =
{
  name: "Cryptography",
  description:
    "computes md5, sha1, sha256, sha384, and sha512 message digests in hex, digest, or base64 format.",
  examples: [
    "Cryptography.md5(:hello)",
    "Cryptography.sha256(:hello, format: :hex)",
    "Cryptography.sha512(:hello, format: :base64)"
  ]
}.freeze
CLASS_FUNCTIONS =
{
  "md5" => {
    name: "md5",
    description:
      "returns an md5 message digest as hex text by default, with digest and base64 formats available.",
    examples: [
      "Cryptography.md5(:hello)",
      "Cryptography.md5(:hello, format: :hex)",
      "Cryptography.md5(:hello, format: :base64)"
    ]
  },
  "sha1" => {
    name: "sha1",
    description:
      "returns a sha1 message digest as hex text by default, with digest and base64 formats available.",
    examples: [
      "Cryptography.sha1(:hello)",
      "Cryptography.sha1(:hello, format: :hex)",
      "Cryptography.sha1(:hello, format: :base64)"
    ]
  },
  "sha256" => {
    name: "sha256",
    description:
      "returns a sha256 message digest as hex text by default, with digest and base64 formats available.",
    examples: [
      "Cryptography.sha256(:hello)",
      "Cryptography.sha256(:hello, format: :hex)",
      "Cryptography.sha256(:hello, format: :base64)"
    ]
  },
  "sha384" => {
    name: "sha384",
    description:
      "returns a sha384 message digest as hex text by default, with digest and base64 formats available.",
    examples: [
      "Cryptography.sha384(:hello)",
      "Cryptography.sha384(:hello, format: :hex)",
      "Cryptography.sha384(:hello, format: :base64)"
    ]
  },
  "sha512" => {
    name: "sha512",
    description:
      "returns a sha512 message digest as hex text by default, with digest and base64 formats available.",
    examples: [
      "Cryptography.sha512(:hello)",
      "Cryptography.sha512(:hello, format: :hex)",
      "Cryptography.sha512(:hello, format: :base64)"
    ]
  }
}.freeze

Constants inherited from Code::Object

INSTANCE_FUNCTIONS, NUMBER_CLASSES

Constants included from Concerns::Shared

Concerns::Shared::COMPOUND_ASSIGNMENT_OPERATORS, Concerns::Shared::OPERATOR_METHOD_ALIASES, Concerns::Shared::SHARED_OPERATORS

Instance Attribute Summary

Attributes included from Concerns::Shared

#functions, #raw

Class Method Summary collapse

Methods inherited from Code::Object

class_documentation, class_functions, code_new, #code_new, documentation, documentation_for, documented_functions_for, function_documentation_for, function_documentation_registry_for, functions, inherited_function_documentation_for, #initialize, instance_functions, maybe, #name, repeat, sorted_dictionary, |

Methods included from Concerns::Shared

#<=>, #==, #as_json, #blank?, #call, #code_and, #code_as_json, #code_blank?, #code_class_functions, #code_compare, #code_deep_duplicate, #code_different, #code_documentable_functions, #code_documentation, #code_duplicate, #code_dynamic_call, #code_equal, #code_exclamation_mark, #code_exclusive_range, #code_false?, #code_falsy?, code_fetch, #code_fetch, #code_functions, code_get, #code_get, #code_greater, #code_greater_or_equal, #code_has_key?, #code_inclusive_range, #code_inspect, #code_instance_functions, #code_instance_of?, #code_is_a?, #code_itself, #code_less, #code_less_or_equal, #code_name, #code_nothing?, #code_or, #code_presence, #code_presence_in, #code_present?, #code_respond_to?, #code_same_object?, #code_self, #code_send, code_set, #code_set, #code_something?, #code_strict_different, #code_strict_equal, #code_tap, #code_then, #code_to_boolean, #code_to_class, #code_to_date, #code_to_decimal, #code_to_dictionary, #code_to_duration, #code_to_integer, #code_to_json, #code_to_list, #code_to_nothing, #code_to_parameter, #code_to_range, #code_to_string, #code_to_time, #code_true?, #code_truthy?, #eql?, #falsy?, #hash, #inspect, #multi_fetch, #nothing?, #present?, #sig, #something?, #succ, #to_code, #to_i, #to_json, #to_s, #truthy?

Constructor Details

This class inherits a constructor from Code::Object

Class Method Details

.call(**args) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/code/object/cryptography.rb', line 75

def self.call(**args)
  code_operator = args.fetch(:operator, nil).to_code
  code_arguments = args.fetch(:arguments, []).to_code
  arguments = code_arguments.raw

  case code_operator.to_s
  when "md5"
    sig(args) { [Object, { format: String.maybe }] }
    code_md5(*arguments)
  when "sha1"
    sig(args) { [Object, { format: String.maybe }] }
    code_sha1(*arguments)
  when "sha256"
    sig(args) { [Object, { format: String.maybe }] }
    code_sha256(*arguments)
  when "sha384"
    sig(args) { [Object, { format: String.maybe }] }
    code_sha384(*arguments)
  when "sha512"
    sig(args) { [Object, { format: String.maybe }] }
    code_sha512(*arguments)
  else
    super
  end
end

.code_md5(*arguments) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/code/object/cryptography.rb', line 101

def self.code_md5(*arguments)
  payload = arguments[0]
  options = arguments[1] || Nothing.new
  code_payload = payload.to_code
  options = options.to_code
  options = Dictionary.new unless options.is_a?(Dictionary)
  code_format = options.code_get("format")
  code_format = String.new("hexdigest") if code_format.nothing?
  format = code_format.to_s.downcase

  case format
  when "hexdigest", "hex"
    String.new(::Digest::MD5.hexdigest(code_payload.to_s))
  when "digest"
    String.new(::Digest::MD5.digest(code_payload.to_s))
  when "base64"
    String.new(
      ::Base64.strict_encode64(::Digest::MD5.digest(code_payload.to_s))
    )
  else
    raise Error, "Cryptography: unknown format #{format.inspect}"
  end
end

.code_sha1(*arguments) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/code/object/cryptography.rb', line 125

def self.code_sha1(*arguments)
  payload = arguments[0]
  options = arguments[1] || Nothing.new
  code_payload = payload.to_code
  options = options.to_code
  options = Dictionary.new unless options.is_a?(Dictionary)
  code_format = options.code_get("format")
  code_format = String.new("hexdigest") if code_format.nothing?
  format = code_format.to_s.downcase

  case format
  when "hexdigest", "hex"
    String.new(::Digest::SHA1.hexdigest(code_payload.to_s))
  when "digest"
    String.new(::Digest::SHA1.digest(code_payload.to_s))
  when "base64"
    String.new(
      ::Base64.strict_encode64(::Digest::SHA1.digest(code_payload.to_s))
    )
  else
    raise Error, "Cryptography: unknown format #{format.inspect}"
  end
end

.code_sha256(*arguments) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/code/object/cryptography.rb', line 149

def self.code_sha256(*arguments)
  payload = arguments[0]
  options = arguments[1] || Nothing.new
  code_payload = payload.to_code
  options = options.to_code
  options = Dictionary.new unless options.is_a?(Dictionary)
  code_format = options.code_get("format")
  code_format = String.new("hexdigest") if code_format.nothing?
  format = code_format.to_s.downcase

  case format
  when "hexdigest", "hex"
    String.new(::Digest::SHA256.hexdigest(code_payload.to_s))
  when "digest"
    String.new(::Digest::SHA256.digest(code_payload.to_s))
  when "base64"
    String.new(
      ::Base64.strict_encode64(::Digest::SHA256.digest(code_payload.to_s))
    )
  else
    raise Error, "Cryptography: unknown format #{format.inspect}"
  end
end

.code_sha384(*arguments) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/code/object/cryptography.rb', line 173

def self.code_sha384(*arguments)
  payload = arguments[0]
  options = arguments[1] || Nothing.new
  code_payload = payload.to_code
  options = options.to_code
  options = Dictionary.new unless options.is_a?(Dictionary)
  code_format = options.code_get("format")
  code_format = String.new("hexdigest") if code_format.nothing?
  format = code_format.to_s.downcase

  case format
  when "hexdigest", "hex"
    String.new(::Digest::SHA384.hexdigest(code_payload.to_s))
  when "digest"
    String.new(::Digest::SHA384.digest(code_payload.to_s))
  when "base64"
    String.new(
      ::Base64.strict_encode64(::Digest::SHA384.digest(code_payload.to_s))
    )
  else
    raise Error, "Cryptography: unknown format #{format.inspect}"
  end
end

.code_sha512(*arguments) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/code/object/cryptography.rb', line 197

def self.code_sha512(*arguments)
  payload = arguments[0]
  options = arguments[1] || Nothing.new
  code_payload = payload.to_code
  options = options.to_code
  options = Dictionary.new unless options.is_a?(Dictionary)
  code_format = options.code_get("format")
  code_format = String.new("hexdigest") if code_format.nothing?
  format = code_format.to_s.downcase

  case format
  when "hexdigest", "hex"
    String.new(::Digest::SHA512.hexdigest(code_payload.to_s))
  when "digest"
    String.new(::Digest::SHA512.digest(code_payload.to_s))
  when "base64"
    String.new(
      ::Base64.strict_encode64(::Digest::SHA512.digest(code_payload.to_s))
    )
  else
    raise Error, "Cryptography: unknown format #{format.inspect}"
  end
end

.function_documentation(scope) ⇒ Object



69
70
71
72
73
# File 'lib/code/object/cryptography.rb', line 69

def self.function_documentation(scope)
  return CLASS_FUNCTIONS if scope == :class

  {}
end