guid.rb 3.0 KB

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