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,365
  • 18
  • 112
  • 133
Timmy
  • 11,978
  • 19
  • 71
  • 104

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
  • 324,247
  • 67
  • 682
  • 662