Skip to content

Instantly share code, notes, and snippets.

@KarlHerler
Created February 14, 2012 20:01
Show Gist options
  • Save KarlHerler/1829739 to your computer and use it in GitHub Desktop.
Save KarlHerler/1829739 to your computer and use it in GitHub Desktop.
A simple hashing function using DES
package com.karlherler.crypto
import com.karlherler.crypto.javax.des.DES
class DESHash(msg: Array[Byte]) {
private val h0 = Array(0x01.toByte, 0x23.toByte, 0x45.toByte,
0x67.toByte, 0x89.toByte, 0xab.toByte,
0xcd.toByte, 0xef.toByte)
val splitted = {
val grouped = msg.grouped(8).toArray
(grouped.dropRight(1) ++ Array(grouped.last.padTo(8, 0.toByte)))
}
val hash = {
splitted.foldLeft(h0) { (h, d) =>
val e = new DES(h)
e.encrypt(d)
}.slice(4, 8)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment