src/Pure/General/sha1.scala
author wenzelm
Fri Nov 25 18:37:14 2011 +0100 (2011-11-25 ago)
changeset 45633 2cb7e34f6096
parent 43714 3749d1e6dde9
child 45667 546d78f0d81f
permissions -rw-r--r--
retain stderr and include it in syslog, which is buffered and thus increases the chance that users see remains from crashes etc.;
     1 /*  Title:      Pure/General/sha1.scala
     2     Author:     Makarius
     3 
     4 Digest strings according to SHA-1 (see RFC 3174).
     5 */
     6 
     7 package isabelle
     8 
     9 
    10 import java.security.MessageDigest
    11 
    12 
    13 object SHA1
    14 {
    15   sealed case class Digest(rep: String)
    16   {
    17     override def toString: String = rep
    18   }
    19 
    20   def digest_bytes(bytes: Array[Byte]): Digest =
    21   {
    22     val result = new StringBuilder
    23     for (b <- MessageDigest.getInstance("SHA").digest(bytes)) {
    24       val i = b.asInstanceOf[Int] & 0xFF
    25       if (i < 16) result += '0'
    26       result ++= Integer.toHexString(i)
    27     }
    28     Digest(result.toString)
    29   }
    30 
    31   def digest(s: String): Digest = digest_bytes(Standard_System.string_bytes(s))
    32 }
    33