Module: IcAgent::Utils
- Included in:
- Candid::TypeIds
- Defined in:
- lib/ic_agent/utils.rb
Class Method Summary collapse
- .encode_list(l) ⇒ Object
- .idl_hash(s) ⇒ Object
-
.label_hash(s) ⇒ Object
used for sort record by key.
- .to_request_id(d) ⇒ Object
Class Method Details
.encode_list(l) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/ic_agent/utils.rb', line 6 def self.encode_list(l) ret = '' l.each do |item| v = item if item.is_a?(Array) v = encode_list(item) end if item.is_a?(Integer) v = LEB128.encode_signed(v) end if item.is_a?(String) v = item end ret += Digest::SHA256.digest(v) end ret end |
.idl_hash(s) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/ic_agent/utils.rb', line 44 def self.idl_hash(s) h = 0 s.bytes.each do |c| h = (h * 223 + c) % 2**32 end h end |
.label_hash(s) ⇒ Object
used for sort record by key
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/ic_agent/utils.rb', line 25 def self.label_hash(s) if s =~ /(^_\d+_$)|(^_0x[0-9a-fA-F]+_$)/ num = s[1..-2] begin if num.start_with?("0x") num = num.to_i(16) else num = num.to_i end rescue # fallback end if num.is_a?(Integer) && num >= 0 && num < 2**32 return num end end idl_hash(s) end |
.to_request_id(d) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/ic_agent/utils.rb', line 52 def self.to_request_id(d) return nil unless d.is_a?(Hash) vec = [] d.each do |k, v| v = encode_list(v) if v.is_a?(Array) v = LEB128.encode_signed(v).string if v.is_a?(Integer) k = k.hex unless k.is_a?(String) v = v.str2hex unless v.is_a?(String) h_k = Digest::SHA256.digest(k) h_v = Digest::SHA256.digest(v) vec << h_k + h_v end s = vec.sort.join Digest::SHA256.digest(s) end |