code-ruby 1.8.8 → 1.8.9

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
  SHA256:
3
- metadata.gz: 616a9d80628394ce57d2decc4da787771d5041584ffb367b4c6ab3e809ba9025
4
- data.tar.gz: 7a4357099a677871fa59d9f8d80981bd238fa4e26e6225bc576ccb969c7cd9ff
3
+ metadata.gz: 2e3f7e0eca6b9a0f46c7173687ec10c4e389565624f96e3de49458ce9449850f
4
+ data.tar.gz: 748a9d98e4f5e2f405b5a9cd4b811c7f3a513a5feb39e9491c3de13499c684c1
5
5
  SHA512:
6
- metadata.gz: 59bd3d036ff0b36835d28adaad432345cd5c1f14e8cd463c656f35a9ef6f505493427a3fbc596d452145c838ac789ec7d682c3f8ffc40433ff1e6dbf0638a449
7
- data.tar.gz: a3d5797753cdf52f35508866721e3f60c527b15c83e863d5fc57e8d6525fe467ec03ed2d92075413d32dad4201812ca8a8186ee71d1be7ab58e43606d26bcfc6
6
+ metadata.gz: 9a0fc23897fdb878e0bfef46439d0f6291818e4931625644dbd6e7ba0f249fa0be24b528c8b999cc1dd800cf8436fe17418e83c6146c74e3687387bed463c426
7
+ data.tar.gz: e7109f12587180228ed2d24f76dd786d1bfb0489060614245810713cfa29c52be0706149b3abe7ab04880069eec89382fa4bde5ddaed78f7c836ed6bc4b77fd8
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- code-ruby (1.8.8)
4
+ code-ruby (1.8.9)
5
5
  activesupport
6
6
  base64
7
7
  bigdecimal
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.8.8
1
+ 1.8.9
@@ -0,0 +1,153 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Code
4
+ class Object
5
+ class Cryptography < Object
6
+ def self.call(**args)
7
+ code_operator = args.fetch(:operator, nil).to_code
8
+ code_arguments = args.fetch(:arguments, []).to_code
9
+ arguments = code_arguments.raw
10
+
11
+ case code_operator.to_s
12
+ when "md5"
13
+ sig(args) { [Object, { format: String.maybe }] }
14
+ code_md5(*arguments)
15
+ when "sha1"
16
+ sig(args) { [Object, { format: String.maybe }] }
17
+ code_sha1(*arguments)
18
+ when "sha256"
19
+ sig(args) { [Object, { format: String.maybe }] }
20
+ code_sha256(*arguments)
21
+ when "sha384"
22
+ sig(args) { [Object, { format: String.maybe }] }
23
+ code_sha384(*arguments)
24
+ when "sha512"
25
+ sig(args) { [Object, { format: String.maybe }] }
26
+ code_sha512(*arguments)
27
+ else
28
+ super
29
+ end
30
+ end
31
+
32
+ def self.code_md5(*arguments)
33
+ payload = arguments[0]
34
+ options = arguments[1] || Nothing.new
35
+ code_payload = payload.to_code
36
+ options = options.to_code
37
+ options = Dictionary.new unless options.is_a?(Dictionary)
38
+ code_format = options.code_get("format")
39
+ code_format = String.new("hexdigest") if code_format.nothing?
40
+ format = code_format.to_s.downcase
41
+
42
+ case format
43
+ when "hexdigest", "hex"
44
+ String.new(::Digest::MD5.hexdigest(code_payload.to_s))
45
+ when "digest"
46
+ String.new(::Digest::MD5.digest(code_payload.to_s))
47
+ when "base64"
48
+ String.new(
49
+ ::Base64.strict_encode64(::Digest::MD5.digest(code_payload.to_s))
50
+ )
51
+ else
52
+ raise Error, "Cryptography: unknown format #{format.inspect}"
53
+ end
54
+ end
55
+
56
+ def self.code_sha1(*arguments)
57
+ payload = arguments[0]
58
+ options = arguments[1] || Nothing.new
59
+ code_payload = payload.to_code
60
+ options = options.to_code
61
+ options = Dictionary.new unless options.is_a?(Dictionary)
62
+ code_format = options.code_get("format")
63
+ code_format = String.new("hexdigest") if code_format.nothing?
64
+ format = code_format.to_s.downcase
65
+
66
+ case format
67
+ when "hexdigest", "hex"
68
+ String.new(::Digest::SHA1.hexdigest(code_payload.to_s))
69
+ when "digest"
70
+ String.new(::Digest::SHA1.digest(code_payload.to_s))
71
+ when "base64"
72
+ String.new(
73
+ ::Base64.strict_encode64(::Digest::SHA1.digest(code_payload.to_s))
74
+ )
75
+ else
76
+ raise Error, "Cryptography: unknown format #{format.inspect}"
77
+ end
78
+ end
79
+
80
+ def self.code_sha256(*arguments)
81
+ payload = arguments[0]
82
+ options = arguments[1] || Nothing.new
83
+ code_payload = payload.to_code
84
+ options = options.to_code
85
+ options = Dictionary.new unless options.is_a?(Dictionary)
86
+ code_format = options.code_get("format")
87
+ code_format = String.new("hexdigest") if code_format.nothing?
88
+ format = code_format.to_s.downcase
89
+
90
+ case format
91
+ when "hexdigest", "hex"
92
+ String.new(::Digest::SHA256.hexdigest(code_payload.to_s))
93
+ when "digest"
94
+ String.new(::Digest::SHA256.digest(code_payload.to_s))
95
+ when "base64"
96
+ String.new(
97
+ ::Base64.strict_encode64(::Digest::SHA256.digest(code_payload.to_s))
98
+ )
99
+ else
100
+ raise Error, "Cryptography: unknown format #{format.inspect}"
101
+ end
102
+ end
103
+
104
+ def self.code_sha384(*arguments)
105
+ payload = arguments[0]
106
+ options = arguments[1] || Nothing.new
107
+ code_payload = payload.to_code
108
+ options = options.to_code
109
+ options = Dictionary.new unless options.is_a?(Dictionary)
110
+ code_format = options.code_get("format")
111
+ code_format = String.new("hexdigest") if code_format.nothing?
112
+ format = code_format.to_s.downcase
113
+
114
+ case format
115
+ when "hexdigest", "hex"
116
+ String.new(::Digest::SHA384.hexdigest(code_payload.to_s))
117
+ when "digest"
118
+ String.new(::Digest::SHA384.digest(code_payload.to_s))
119
+ when "base64"
120
+ String.new(
121
+ ::Base64.strict_encode64(::Digest::SHA384.digest(code_payload.to_s))
122
+ )
123
+ else
124
+ raise Error, "Cryptography: unknown format #{format.inspect}"
125
+ end
126
+ end
127
+
128
+ def self.code_sha512(*arguments)
129
+ payload = arguments[0]
130
+ options = arguments[1] || Nothing.new
131
+ code_payload = payload.to_code
132
+ options = options.to_code
133
+ options = Dictionary.new unless options.is_a?(Dictionary)
134
+ code_format = options.code_get("format")
135
+ code_format = String.new("hexdigest") if code_format.nothing?
136
+ format = code_format.to_s.downcase
137
+
138
+ case format
139
+ when "hexdigest", "hex"
140
+ String.new(::Digest::SHA512.hexdigest(code_payload.to_s))
141
+ when "digest"
142
+ String.new(::Digest::SHA512.digest(code_payload.to_s))
143
+ when "base64"
144
+ String.new(
145
+ ::Base64.strict_encode64(::Digest::SHA512.digest(code_payload.to_s))
146
+ )
147
+ else
148
+ raise Error, "Cryptography: unknown format #{format.inspect}"
149
+ end
150
+ end
151
+ end
152
+ end
153
+ end
@@ -148,6 +148,13 @@ class Code
148
148
  else
149
149
  Class.new(Base64)
150
150
  end
151
+ when "Cryptography"
152
+ sig(args) { Object.repeat }
153
+ if code_arguments.any?
154
+ Cryptography.new(*code_arguments.raw)
155
+ else
156
+ Class.new(Cryptography)
157
+ end
151
158
  when "Json"
152
159
  sig(args) { Object.repeat }
153
160
  code_arguments.any? ? Json.new(*code_arguments.raw) : Class.new(Json)
data/lib/code-ruby.rb CHANGED
@@ -6,6 +6,7 @@ require "bigdecimal"
6
6
  require "bigdecimal/util"
7
7
  require "date"
8
8
  require "did_you_mean"
9
+ require "digest"
9
10
  require "json"
10
11
  require "language-ruby"
11
12
  require "mail"
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ RSpec.describe Code::Object::Cryptography do
6
+ it "returns sha256 hexdigest by default" do
7
+ expect(Code.evaluate("Cryptography.sha256(:hello)").to_s).to eq(
8
+ Digest::SHA256.hexdigest("hello")
9
+ )
10
+ end
11
+
12
+ it "supports base64 format for sha1" do
13
+ expected = Base64.strict_encode64(Digest::SHA1.digest("hello"))
14
+
15
+ expect(
16
+ Code.evaluate("Cryptography.sha1(:hello, format: :base64)").to_s
17
+ ).to eq(expected)
18
+ end
19
+
20
+ it "raises on unknown formats" do
21
+ expect do
22
+ Code.evaluate("Cryptography.sha256(:hello, format: :unknown)")
23
+ end.to raise_error(Code::Error, /unknown format/i)
24
+ end
25
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: code-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.8.8
4
+ version: 1.8.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dorian Marié
@@ -264,6 +264,7 @@ files:
264
264
  - lib/code/object/class.rb
265
265
  - lib/code/object/code.rb
266
266
  - lib/code/object/context.rb
267
+ - lib/code/object/cryptography.rb
267
268
  - lib/code/object/date.rb
268
269
  - lib/code/object/decimal.rb
269
270
  - lib/code/object/dictionary.rb
@@ -336,6 +337,7 @@ files:
336
337
  - package.json
337
338
  - spec/code/node/call_spec.rb
338
339
  - spec/code/object/boolean_spec.rb
340
+ - spec/code/object/cryptography_spec.rb
339
341
  - spec/code/object/decimal_spec.rb
340
342
  - spec/code/object/dictionary_spec.rb
341
343
  - spec/code/object/function_spec.rb