src/Pure/General/sha1.scala
author wenzelm
Fri, 28 Feb 2014 12:04:40 +0100
changeset 55802 f7ceebe2f1b5
parent 54440 2c4940d2edf7
child 57638 ed58e740a699
permissions -rw-r--r--
prefer abstract datatype -- in accordance to ML version;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
38473
bd96f2a5beb0 digesting strings according to SHA-1 -- Scala version;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/General/sha1.scala
45673
cd41e3903fbf separate compilation of PIDE vs. Pure sources, which enables independent Scala library;
wenzelm
parents: 45667
diff changeset
     2
    Module:     PIDE
38473
bd96f2a5beb0 digesting strings according to SHA-1 -- Scala version;
wenzelm
parents:
diff changeset
     3
    Author:     Makarius
bd96f2a5beb0 digesting strings according to SHA-1 -- Scala version;
wenzelm
parents:
diff changeset
     4
41954
fb94df4505a0 explicit type SHA1.digest;
wenzelm
parents: 38473
diff changeset
     5
Digest strings according to SHA-1 (see RFC 3174).
38473
bd96f2a5beb0 digesting strings according to SHA-1 -- Scala version;
wenzelm
parents:
diff changeset
     6
*/
bd96f2a5beb0 digesting strings according to SHA-1 -- Scala version;
wenzelm
parents:
diff changeset
     7
bd96f2a5beb0 digesting strings according to SHA-1 -- Scala version;
wenzelm
parents:
diff changeset
     8
package isabelle
bd96f2a5beb0 digesting strings according to SHA-1 -- Scala version;
wenzelm
parents:
diff changeset
     9
bd96f2a5beb0 digesting strings according to SHA-1 -- Scala version;
wenzelm
parents:
diff changeset
    10
48409
0d2114eb412a more explicit java.io.{File => JFile};
wenzelm
parents: 48343
diff changeset
    11
import java.io.{File => JFile, FileInputStream}
38473
bd96f2a5beb0 digesting strings according to SHA-1 -- Scala version;
wenzelm
parents:
diff changeset
    12
import java.security.MessageDigest
bd96f2a5beb0 digesting strings according to SHA-1 -- Scala version;
wenzelm
parents:
diff changeset
    13
bd96f2a5beb0 digesting strings according to SHA-1 -- Scala version;
wenzelm
parents:
diff changeset
    14
bd96f2a5beb0 digesting strings according to SHA-1 -- Scala version;
wenzelm
parents:
diff changeset
    15
object SHA1
bd96f2a5beb0 digesting strings according to SHA-1 -- Scala version;
wenzelm
parents:
diff changeset
    16
{
55802
f7ceebe2f1b5 prefer abstract datatype -- in accordance to ML version;
wenzelm
parents: 54440
diff changeset
    17
  final class Digest private[SHA1](val rep: String)
41954
fb94df4505a0 explicit type SHA1.digest;
wenzelm
parents: 38473
diff changeset
    18
  {
55802
f7ceebe2f1b5 prefer abstract datatype -- in accordance to ML version;
wenzelm
parents: 54440
diff changeset
    19
    override def hashCode: Int = rep.hashCode
f7ceebe2f1b5 prefer abstract datatype -- in accordance to ML version;
wenzelm
parents: 54440
diff changeset
    20
    override def equals(that: Any): Boolean =
f7ceebe2f1b5 prefer abstract datatype -- in accordance to ML version;
wenzelm
parents: 54440
diff changeset
    21
      that match {
f7ceebe2f1b5 prefer abstract datatype -- in accordance to ML version;
wenzelm
parents: 54440
diff changeset
    22
        case other: Digest => rep == other.rep
f7ceebe2f1b5 prefer abstract datatype -- in accordance to ML version;
wenzelm
parents: 54440
diff changeset
    23
        case _ => false
f7ceebe2f1b5 prefer abstract datatype -- in accordance to ML version;
wenzelm
parents: 54440
diff changeset
    24
      }
41954
fb94df4505a0 explicit type SHA1.digest;
wenzelm
parents: 38473
diff changeset
    25
    override def toString: String = rep
fb94df4505a0 explicit type SHA1.digest;
wenzelm
parents: 38473
diff changeset
    26
  }
fb94df4505a0 explicit type SHA1.digest;
wenzelm
parents: 38473
diff changeset
    27
48343
3060e6343953 more SHA1.digest operations;
wenzelm
parents: 45673
diff changeset
    28
  private def make_result(digest: MessageDigest): Digest =
38473
bd96f2a5beb0 digesting strings according to SHA-1 -- Scala version;
wenzelm
parents:
diff changeset
    29
  {
bd96f2a5beb0 digesting strings according to SHA-1 -- Scala version;
wenzelm
parents:
diff changeset
    30
    val result = new StringBuilder
48343
3060e6343953 more SHA1.digest operations;
wenzelm
parents: 45673
diff changeset
    31
    for (b <- digest.digest()) {
38473
bd96f2a5beb0 digesting strings according to SHA-1 -- Scala version;
wenzelm
parents:
diff changeset
    32
      val i = b.asInstanceOf[Int] & 0xFF
bd96f2a5beb0 digesting strings according to SHA-1 -- Scala version;
wenzelm
parents:
diff changeset
    33
      if (i < 16) result += '0'
bd96f2a5beb0 digesting strings according to SHA-1 -- Scala version;
wenzelm
parents:
diff changeset
    34
      result ++= Integer.toHexString(i)
bd96f2a5beb0 digesting strings according to SHA-1 -- Scala version;
wenzelm
parents:
diff changeset
    35
    }
55802
f7ceebe2f1b5 prefer abstract datatype -- in accordance to ML version;
wenzelm
parents: 54440
diff changeset
    36
    new Digest(result.toString)
38473
bd96f2a5beb0 digesting strings according to SHA-1 -- Scala version;
wenzelm
parents:
diff changeset
    37
  }
bd96f2a5beb0 digesting strings according to SHA-1 -- Scala version;
wenzelm
parents:
diff changeset
    38
48409
0d2114eb412a more explicit java.io.{File => JFile};
wenzelm
parents: 48343
diff changeset
    39
  def digest(file: JFile): Digest =
48343
3060e6343953 more SHA1.digest operations;
wenzelm
parents: 45673
diff changeset
    40
  {
3060e6343953 more SHA1.digest operations;
wenzelm
parents: 45673
diff changeset
    41
    val stream = new FileInputStream(file)
3060e6343953 more SHA1.digest operations;
wenzelm
parents: 45673
diff changeset
    42
    val digest = MessageDigest.getInstance("SHA")
3060e6343953 more SHA1.digest operations;
wenzelm
parents: 45673
diff changeset
    43
3060e6343953 more SHA1.digest operations;
wenzelm
parents: 45673
diff changeset
    44
    val buf = new Array[Byte](65536)
3060e6343953 more SHA1.digest operations;
wenzelm
parents: 45673
diff changeset
    45
    var m = 0
3060e6343953 more SHA1.digest operations;
wenzelm
parents: 45673
diff changeset
    46
    try {
3060e6343953 more SHA1.digest operations;
wenzelm
parents: 45673
diff changeset
    47
      do {
3060e6343953 more SHA1.digest operations;
wenzelm
parents: 45673
diff changeset
    48
        m = stream.read(buf, 0, buf.length)
3060e6343953 more SHA1.digest operations;
wenzelm
parents: 45673
diff changeset
    49
        if (m != -1) digest.update(buf, 0, m)
3060e6343953 more SHA1.digest operations;
wenzelm
parents: 45673
diff changeset
    50
      } while (m != -1)
3060e6343953 more SHA1.digest operations;
wenzelm
parents: 45673
diff changeset
    51
    }
3060e6343953 more SHA1.digest operations;
wenzelm
parents: 45673
diff changeset
    52
    finally { stream.close }
3060e6343953 more SHA1.digest operations;
wenzelm
parents: 45673
diff changeset
    53
3060e6343953 more SHA1.digest operations;
wenzelm
parents: 45673
diff changeset
    54
    make_result(digest)
3060e6343953 more SHA1.digest operations;
wenzelm
parents: 45673
diff changeset
    55
  }
3060e6343953 more SHA1.digest operations;
wenzelm
parents: 45673
diff changeset
    56
3060e6343953 more SHA1.digest operations;
wenzelm
parents: 45673
diff changeset
    57
  def digest(bytes: Array[Byte]): Digest =
3060e6343953 more SHA1.digest operations;
wenzelm
parents: 45673
diff changeset
    58
  {
3060e6343953 more SHA1.digest operations;
wenzelm
parents: 45673
diff changeset
    59
    val digest = MessageDigest.getInstance("SHA")
3060e6343953 more SHA1.digest operations;
wenzelm
parents: 45673
diff changeset
    60
    digest.update(bytes)
3060e6343953 more SHA1.digest operations;
wenzelm
parents: 45673
diff changeset
    61
3060e6343953 more SHA1.digest operations;
wenzelm
parents: 45673
diff changeset
    62
    make_result(digest)
3060e6343953 more SHA1.digest operations;
wenzelm
parents: 45673
diff changeset
    63
  }
3060e6343953 more SHA1.digest operations;
wenzelm
parents: 45673
diff changeset
    64
54440
2c4940d2edf7 tuned signature;
wenzelm
parents: 50203
diff changeset
    65
  def digest(bytes: Bytes): Digest = bytes.sha1_digest
2c4940d2edf7 tuned signature;
wenzelm
parents: 50203
diff changeset
    66
2c4940d2edf7 tuned signature;
wenzelm
parents: 50203
diff changeset
    67
  def digest(string: String): Digest = digest(Bytes(string))
38473
bd96f2a5beb0 digesting strings according to SHA-1 -- Scala version;
wenzelm
parents:
diff changeset
    68
}
bd96f2a5beb0 digesting strings according to SHA-1 -- Scala version;
wenzelm
parents:
diff changeset
    69