guid.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. class Ldap
  2. # Class for handling LDAP GUIDs.
  3. # strongly inspired by
  4. # https://gist.github.com/astockwell/359c950fbc650c339eea
  5. # Big thanks to @astockwell
  6. class Guid
  7. # Checks if the given string is a valid GUID.
  8. #
  9. # @param string [String] The string that should be checked for valid GUID format.
  10. #
  11. # @example
  12. # Ldap::Guid.valid?('f742b361-32c6-4a92-baaa-eaae7df657ee')
  13. # #=> true
  14. #
  15. # @return [Boolean]
  16. def self.valid?(string)
  17. string.match?(/\w{8}\-\w{4}\-\w{4}\-\w{4}\-\w+/)
  18. end
  19. # Convers a given GUID string into the HEX equivalent.
  20. #
  21. # @param string [String] The GUID string that should converted into HEX.
  22. #
  23. # @example
  24. # Ldap::Guid.hex('f742b361-32c6-4a92-baaa-eaae7df657ee')
  25. # #=> "a\xB3B\xF7\xC62\x92J\xBA\xAA\xEA\xAE}\xF6W\xEE".b
  26. #
  27. # @return [String] The HEX equivalent of the given GUID string.
  28. def self.hex(string)
  29. new(string).hex
  30. end
  31. # Convers a given HEX string into the GUID equivalent.
  32. #
  33. # @param string [String] The HEX string that should converted into a GUID.
  34. #
  35. # @example
  36. # Ldap::Guid.string("a\xB3B\xF7\xC62\x92J\xBA\xAA\xEA\xAE}\xF6W\xEE".b)
  37. # #=> 'f742b361-32c6-4a92-baaa-eaae7df657ee'
  38. #
  39. # @return [String] The GUID equivalent of the given HEX string.
  40. def self.string(hex)
  41. new(hex).string
  42. end
  43. # Initializes an instance for the LDAP::Guid class to convert from/to HEX and GUID strings.
  44. #
  45. # @param string [String] The HEX or GUID string that should converted.
  46. #
  47. # @example
  48. # guid = Ldap::Guid.new('f742b361-32c6-4a92-baaa-eaae7df657ee')
  49. #
  50. # @return [nil]
  51. def initialize(guid)
  52. @guid = guid
  53. end
  54. # Convers the GUID string into the HEX equivalent.
  55. #
  56. # @example
  57. # guid.hex
  58. # #=> "a\xB3B\xF7\xC62\x92J\xBA\xAA\xEA\xAE}\xF6W\xEE".b
  59. #
  60. # @return [String] The HEX equivalent of the GUID string.
  61. def hex
  62. [oracle_raw16(guid)].pack('H*')
  63. end
  64. # Convers the HEX string into the GUID equivalent.
  65. #
  66. # @example
  67. # guid.string
  68. # #=> 'f742b361-32c6-4a92-baaa-eaae7df657ee'
  69. #
  70. # @return [String] The GUID equivalent of the HEX string.
  71. def string
  72. oracle_raw16(guid.unpack('H*').first, dashify: true)
  73. end
  74. private
  75. attr_reader :guid
  76. def oracle_raw16(string, dashify: false)
  77. # remove dashes
  78. string.delete!('-')
  79. # split every two chars
  80. parts = string.scan(/.{1,2}/)
  81. # re-order according to oracle format index and join
  82. oracle_format_indices = [3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11, 12, 13, 14, 15]
  83. result = oracle_format_indices.map { |index| parts[index] }.join('')
  84. # add dashes if requested
  85. return result if !dashify
  86. [
  87. result[0..7],
  88. result[8..11],
  89. result[12..15],
  90. result[16..19],
  91. result[20..result.size]
  92. ].join('-')
  93. end
  94. end
  95. end