-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.rb
More file actions
executable file
·320 lines (265 loc) · 7.32 KB
/
script.rb
File metadata and controls
executable file
·320 lines (265 loc) · 7.32 KB
1
2
3
4
5
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/usr/bin/env ruby
require 'pp'
require 'cryptopp'
module ABNF
IANAToken = '[a-zA-Z\d\-]+?'
Cr = "\u000d"
Lf = "\u000a"
Crlf = "(#{Cr}|#{Lf})"
Utf8_tail = '[\u0080-\u00bf]'
Utf8_2 = '([\u00c2-\u00df]|' + "#{Utf8_tail})"
Utf8_3 = '([\u00e0\u00a0-\u00bf\u00e1-\u00ec\u00ed\u0080-\u009f\u00ee-\u00ef]|' + "#{Utf8_tail})"
Utf8_4 = '([\u00f0\u0090-\u00bf\u00f1-\u00f3\u00f4\u0080-\u008f]|' + "#{Utf8_tail})"
Wsp = '[ \t]'
VChar = '[\u0021-\u007e]'
NonASCII = "(#{Utf8_2}|#{Utf8_3}|#{Utf8_4})"
QSafeChar = "(#{Wsp}|" + '[!\u0023-\u007e]' + "|#{NonASCII})"
SafeChar = "(#{Wsp}|" + '[!\u0023-\u0039\u003c-\u007e]' + "|#{NonASCII})"
ValueChar = "(#{Wsp}|#{VChar}|#{NonASCII})"
DQuote = '"'
PText = "#{SafeChar}*?"
QuotedString = "#{DQuote}(#{QSafeChar}*?)#{DQuote}"
XName = "[xX]-#{IANAToken}"
Group = IANAToken
Name = "(#{XName}|#{IANAToken})"
ParamName = "(#{XName}|#{IANAToken})"
ParamValue = "(#{PText}|#{QuotedString})"
PValueList = "(?<head>#{ParamValue})(?<tail>(,#{ParamValue})*)"
Pid = '\d+(\.\d+)*'
PidList = "(?<head>#{Pid})(?<tail>(,#{Pid})*)"
Param = "(?<pname>#{ParamName})=(?<pvalue>#{PValueList})"
Params = "(;(?<phead>#{Param}))(?<ptail>(;#{Param})*)"
Value = "#{ValueChar}*?"
LineGroup = "((?<group>#{Group})" + '\.' + ")?"
Contentline = "#{LineGroup}(?<key>#{Name})(?<params>(#{Params})?):(?<value>#{Value})#{Crlf}"
BeginLine = "BEGIN:(?<component>#{VChar}+)#{Crlf}"
EndLine = "END:#{VChar}+#{Crlf}"
VersionLine = "VERSION:(?<version>#{VChar}+)#{Crlf}"
Vcard = "#{BeginLine}#{VersionLine}(#{Contentline})+#{EndLine}"
Component = "#{BeginLine}(?<body>.*)#{EndLine}"
ComponentRegex = /\A#{ABNF::Component}\Z/m
VcardRegex = /\A#{ABNF::Vcard}\Z/
ContentlineRegex = /\A#{ABNF::Contentline}\Z/
ParamsRegex = /\A#{ABNF::Params}\Z/
ParamRegex = /\A#{ABNF::Param}\Z/
end
class Component
attr_accessor :name, :body, :properties
def initialize(string)
matched = ABNF::ComponentRegex.match(string)
@name = matched[:component]
@body = matched[:body]
end
def parse_properties
@properties = []
# TODO: simplistic splitting
@body.each_line do |line|
@properties << ComponentProperty.new(line)
end
end
def to_hash_string
"BEGIN:#{@name}:CHECKSUM
#{properties_to_hash_string}
END:#{@name}:CHECKSUM"
end
def properties_to_hash_string
list_to_text(
@properties.map do |prop|
prop.normalized_hash
end.sort,
"\r\n"
)
end
def hash
calculate_hash to_hash_string
end
end
class ComponentProperty
attr_accessor :key, :value, :params, :value_type
def initialize(string)
raise ArgumentError.new("No input string provided") unless string
matched = ABNF::ContentlineRegex.match(string)
pp string
#puts "ComponentProperty: string #{string}"
#puts "ComponentProperty: matched #{matched.inspect}"
# pp "matched are"
# pp matched
@key = matched[:key].upcase
@value = matched[:value]
@params = {}
case @key
when "ADR", "N"
@value = @value.split(";")
when "CATEGORY", "NICKNAME"
@value = @value.split(",")
else
@value = [@value]
end
params = matched[:params]
# Simplistic assumption: by default VALUE=TEXT
value_type = PropertyParameter.new("VALUE=TEXT")
@params[value_type.name] = value_type
# TODO: simplistic splitting of params
params.split(";").each do |param|
#puts "params is #{param}"
next if param.empty?
param = PropertyParameter.new(param)
pk = param.name
@params[pk] = param
end
@value_type = @params["VALUE"].value.first
@params.delete("VALUE")
end
def hash
calculate_hash to_hash_string
end
def normalized_hash
"#{@key}:#{hash}"
end
def to_hash_string
"#{@key}:#{@value_type}/" +
(@value.is_a?(Array) ? list_to_text(@value, ";") : @value) +
"?#{sorted_parameters_to_hash_string}"
end
def sorted_parameters
@params.sort_by do |key, obj|
key
end.map do |arr|
arr.last
end
end
def sorted_parameters_to_hash_string
param_string = sorted_parameters.map do |param|
param.to_hash_string
end
"##{list_to_text(param_string, ";")}"
end
end
# class PropertyParameters
#
# attr_accessor :mapping
#
# def initialize(string)
# matched = ABNF::ParamsRegex.match(string)
# puts "PropertyParameter: string #{string}"
#
# @key = matched[:key]
# @value = matched[:value]
#
# end
# end
class PropertyParameter
attr_accessor :name, :value
def initialize(string)
raise ArgumentError.new("No input string provided") unless string
matched = ABNF::ParamRegex.match(string)
#puts "PropertyParameter: string #{string}"
#puts "PropertyParameter: matched #{matched.inspect}"
@name = matched[:pname].upcase
# TODO: simplistic splitting
@value = matched[:pvalue]
if valuematch = /\A"(.*)"\Z/.match(@value)
@value = valuematch[1]
end
# Upcase the property value type
if @name == "VALUE"
@value = [@value.upcase]
else
@value = @value.split(",")
end
end
def to_hash_string
"{#{@name}:#{list_to_text(@value.sort, ";")}}"
end
end
def calculate_hash(s, alg = HASH_FUNCTION_NAME)
calculate_hash_cryptopp(s, alg)
end
def calculate_hash_cryptopp(s, alg)
cryptopp_alg = case alg
when :sha256, :sha384, :sha512
alg
when :sha512224
when :sha512256
when :sha3224
:sha3_224
when :sha3256
:sha3_256
when :sha3384
:sha3_384
when :sha3512
:sha3_512
when :blake2b
alg
when :whirlpool
alg
when :streebog512
when :streebog256
when :ripemd256, :ripemd320
alg
when :sm3
end
unless CryptoPP.digest_enabled? cryptopp_alg
raise ArgumentError.new("No hash function #{cryptopp_alg} available")
end
CryptoPP.digest_factory(cryptopp_alg, s)
end
#require 'digest'
#require 'sha3-pure-ruby'
#require 'digest/whirlpool'
def calculate_hash_original(s, alg)
klass = case alg
when :sha256
Digest::SHA256
when :sha384
Digest::SHA384
when :sha512
Digest::SHA512
when :sha3224
Digest::SHA3.new(224)
when :sha3384
Digest::SHA3.new(384)
when :sha3512
Digest::SHA3.new(512)
when :whirlpool
# Currently fails
Digest::Whirlpool
end
klass.hexdigest s
end
def list_to_text(array, delimiter)
"#{array.join(delimiter || "")}"
end
data = 'BEGIN:VCARD
VERSION:4.0
KIND:individual
FN:Martin Van Buren
N:Van Buren;Martin;;;Hon.
TEL;VALUE=uri;PREF=1;TYPE="voice,home":tel:+1-888-888-8888;ext=8888
CHECKSUM;VALUE=TEXT;TYPE=sha512:
END:VCARD
'
# Choose your hash function here
HASH_FUNCTION_NAME = :whirlpool
HASH_FUNCTION_NAME = :ripemd256
HASH_FUNCTION_NAME = :blake2b
HASH_FUNCTION_NAME = :sha3256
component = Component.new(data)
#pp component.body
component.parse_properties
#pp component
#pp component.properties
component.properties.map do |prop|
#pp prop.params
#pp prop.value_type
puts "1. Hash string for property #{prop.key}"
puts prop.to_hash_string
end
component.properties.map do |prop|
puts "2. Normalized hash for property #{prop.key}"
puts prop.normalized_hash
end
puts "3. Component hash string is:"
puts component.to_hash_string
puts "4. Component hash is:"
puts component.hash