4

I want to md5 an Array[Long], so I would like to make that an Array[Byte] because the MD5 function takes an Array[Byte], how can I do that?

I use messagedigest for this.

kiritsuku
  • 52,967
  • 18
  • 114
  • 136
Timmy
  • 12,468
  • 20
  • 77
  • 107

1 Answers1

6

Using ByteBuffer:

val arr = listOfLongs.
  foldLeft(ByteBuffer.allocate(8 * listOfLongs.size)){ (buffer, lon) => 
    buffer putLong lon
  }.array

Or more imperatively:

val buffer = ByteBuffer.allocate(8 * listOfLongs.size)
listOfLongs.foreach(buffer putLong _)
val arr = buffer.array

Note: if you need little-endian, just call:

buffer.order(java.nio.ByteOrder.LITTLE_ENDIAN)

at the beginning. Fore more inspiration: Convert long to byte array and add it to another array.

Community
  • 1
  • 1
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674