Class: Code::Object::Cryptography

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

Constant Summary

Constants inherited from Code::Object

NUMBER_CLASSES

Instance Attribute Summary

Attributes included from Concerns::Shared

#methods, #raw

Class Method Summary collapse

Methods inherited from Code::Object

code_new, #code_new, #initialize, maybe, #name, repeat, |

Methods included from Concerns::Shared

#<=>, #==, #as_json, #blank?, #call, #code_and, #code_as_json, #code_blank?, #code_compare, #code_deep_duplicate, #code_different, #code_duplicate, #code_equal, #code_exclamation_mark, #code_exclusive_range, #code_falsy?, #code_fetch, code_fetch, code_get, #code_get, #code_greater, #code_greater_or_equal, #code_inclusive_range, #code_inspect, #code_less, #code_less_or_equal, #code_methods, #code_name, #code_nothing?, #code_or, #code_presence, #code_presence_in, #code_present?, #code_self, code_set, #code_set, #code_something?, #code_strict_different, #code_strict_equal, #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_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



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/code/object/cryptography.rb', line 6

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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/code/object/cryptography.rb', line 32

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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/code/object/cryptography.rb', line 56

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



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

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



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

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



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

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