spring-ldap icon indicating copy to clipboard operation
spring-ldap copied to clipboard

Add default UUID to byte array converter with little endian support

Open goto1134 opened this issue 3 years ago • 1 comments
trafficstars

Since all binary data is stored in little endian format in ldap, it is not very obvious that the following objectGUID 90d1c68e-01be-4485-aafd-b3ffb9ddb026 should be converted to an octet string \8e\c6\d1\90\be\01\85\44\aa\fd\b3\ff\b9\dd\b0\26.

Please, provide the default converters for byte[] <-> java.util.UUID transformations. Here are some of my ideas for these converters:

@Component
class ByteArrayToUuidConverter : Converter<ByteArray, UUID> {
    override fun convert(guidBytes: ByteArray): UUID {
        require(guidBytes.size == 16) {
            "Guid bytes must be 16 byte long, but was ${guidBytes.size}"
        }
        val bytes = guidBytes.copyOf().apply {
            reverse(0, 4)
            reverse(4, 6)
            reverse(6, 8)
        }
        return ByteBuffer.wrap(bytes).let { UUID(it.long, it.long) }
    }
}

@Component
class UuidToByteArrayConverter : Converter<UUID, ByteArray> {
    override fun convert(source: UUID): ByteArray {
        val byteBuffer = ByteBuffer.wrap(ByteArray(16)).apply {
            putLong(source.mostSignificantBits)
            putLong(source.leastSignificantBits)
        }
        val guidBytes = byteBuffer.array().apply {
            reverse(0, 4)
            reverse(4, 6)
            reverse(6, 8)
        }
        return guidBytes
    }
}

goto1134 avatar Apr 10 '22 10:04 goto1134

This issue is also related to https://github.com/spring-projects/spring-ldap/issues/633

goto1134 avatar Apr 10 '22 10:04 goto1134