Skip to content

Instantly share code, notes, and snippets.

@andy1138
Created February 25, 2013 07:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andy1138/5028225 to your computer and use it in GitHub Desktop.
Save andy1138/5028225 to your computer and use it in GitHub Desktop.
package lsug
import java.security.MessageDigest
import java.io.{BufferedInputStream, FileInputStream}
object SchemaString extends App {
implicit class SchemaHelper(val sc: StringContext) extends AnyVal {
def schema(a: Any*): String = sc.parts.head.split('\n').map( _.trim).mkString("\n")
}
val s = schema"""@totalColumns 3
Name:
Age:"""
println(s)
// https://github.com/lift/framework/blob/master/core/util/src/main/scala/net/liftweb/util/SecurityHelpers.scala
/** encode a Byte array as hexadecimal characters */
def hexEncode(in: Array[Byte]): String = {
val sb = new StringBuilder
val len = in.length
def addDigit(in: Array[Byte], pos: Int, len: Int, sb: StringBuilder) {
if (pos < len) {
val b: Int = in(pos)
val msb = (b & 0xf0) >> 4
val lsb = (b & 0x0f)
sb.append((if (msb < 10) ('0' + msb).asInstanceOf[Char] else ('a' + (msb - 10)).asInstanceOf[Char]))
sb.append((if (lsb < 10) ('0' + lsb).asInstanceOf[Char] else ('a' + (lsb - 10)).asInstanceOf[Char]))
addDigit(in, pos + 1, len, sb)
}
}
addDigit(in, 0, len, sb)
sb.toString
}
/** create a MD5 digest from a Byte array */
val fileName = """/Users/andy/Downloads/scala-docs-2.10.0.zip"""
val digest = MessageDigest.getInstance("MD5")
val file = new BufferedInputStream(new FileInputStream(fileName))
Stream.continually(file.read).takeWhile(-1 !=).map(_.toByte).foreach( digest.update(_))
val hexStr = hexEncode(digest.digest)
val same = hexStr == "699d61aff25f16a5560372e610da91ab"
println( s"Hex: $hexStr $same")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment