Class: Code::Object::Cryptography

Inherits:
Object
  • 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

Class Method Summary collapse

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