Module: UUID47
- Defined in:
- lib/uuid47.rb,
lib/uuid47/version.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
- VERSION =
"0.1.0"
Class Method Summary collapse
- .decode_v4facade(v4facade, key) ⇒ Object
-
.encode_v4facade(id_v7, key) ⇒ Object
Your code goes here...
- .mask48(raw, key) ⇒ Object
- .set_version(uuid_str, version) ⇒ Object
- .xor_strings(a, b) ⇒ Object
Class Method Details
.decode_v4facade(v4facade, key) ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/uuid47.rb', line 22 def self.decode_v4facade(v4facade, key) uuid_str = UUIDTools::UUID.parse(v4facade).raw mask = self.mask48(uuid_str, key) # (de)apply mask new_uuid_str = self.xor_strings(mask, uuid_str[0..5]) + uuid_str[6..] # then set version self.set_version(new_uuid_str, 7) # then convert to UUID string UUIDTools::UUID.parse_raw(new_uuid_str).to_str end |
.encode_v4facade(id_v7, key) ⇒ Object
Your code goes here...
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/uuid47.rb', line 11 def self.encode_v4facade(id_v7, key) uuid_str = UUIDTools::UUID.parse(id_v7).raw mask = self.mask48(uuid_str, key) # apply mask new_uuid_str = self.xor_strings(mask, uuid_str[0..5]) + uuid_str[6..] # then set version self.set_version(new_uuid_str, 4) # then convert to UUID string UUIDTools::UUID.parse_raw(new_uuid_str).to_str end |
.mask48(raw, key) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/uuid47.rb', line 34 def self.mask48(raw, key) msg = '' msg += (raw[6].ord & 0x0f).chr msg += raw[7] msg += (raw[8].ord & 0x3F).chr msg += raw[9..15] mask_int = SipHash.digest(key, msg) mask = '' mask += (mask_int >> 40 & 0xff).chr mask += (mask_int >> 32 & 0xff).chr mask += (mask_int >> 24 & 0xff).chr mask += (mask_int >> 16 & 0xff).chr mask += (mask_int >> 8 & 0xff).chr mask += (mask_int >> 0 & 0xff).chr mask end |
.set_version(uuid_str, version) ⇒ Object
55 56 57 |
# File 'lib/uuid47.rb', line 55 def self.set_version(uuid_str, version) uuid_str[6] = ((uuid_str[6].ord & 0x0f) | ((version & 0x0f) << 4)).chr end |
.xor_strings(a, b) ⇒ Object
51 52 53 |
# File 'lib/uuid47.rb', line 51 def self.xor_strings(a, b) a.bytes.zip(b.bytes).map { |x, y| (x ^ y).chr }.join end |