author | wenzelm |
Fri, 28 Feb 2014 12:04:40 +0100 | |
changeset 55802 | f7ceebe2f1b5 |
parent 54440 | 2c4940d2edf7 |
child 57638 | ed58e740a699 |
permissions | -rw-r--r-- |
38473 | 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 | 3 |
Author: Makarius |
4 |
||
41954 | 5 |
Digest strings according to SHA-1 (see RFC 3174). |
38473 | 6 |
*/ |
7 |
||
8 |
package isabelle |
|
9 |
||
10 |
||
48409 | 11 |
import java.io.{File => JFile, FileInputStream} |
38473 | 12 |
import java.security.MessageDigest |
13 |
||
14 |
||
15 |
object SHA1 |
|
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 | 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 | 25 |
override def toString: String = rep |
26 |
} |
|
27 |
||
48343 | 28 |
private def make_result(digest: MessageDigest): Digest = |
38473 | 29 |
{ |
30 |
val result = new StringBuilder |
|
48343 | 31 |
for (b <- digest.digest()) { |
38473 | 32 |
val i = b.asInstanceOf[Int] & 0xFF |
33 |
if (i < 16) result += '0' |
|
34 |
result ++= Integer.toHexString(i) |
|
35 |
} |
|
55802
f7ceebe2f1b5
prefer abstract datatype -- in accordance to ML version;
wenzelm
parents:
54440
diff
changeset
|
36 |
new Digest(result.toString) |
38473 | 37 |
} |
38 |
||
48409 | 39 |
def digest(file: JFile): Digest = |
48343 | 40 |
{ |
41 |
val stream = new FileInputStream(file) |
|
42 |
val digest = MessageDigest.getInstance("SHA") |
|
43 |
||
44 |
val buf = new Array[Byte](65536) |
|
45 |
var m = 0 |
|
46 |
try { |
|
47 |
do { |
|
48 |
m = stream.read(buf, 0, buf.length) |
|
49 |
if (m != -1) digest.update(buf, 0, m) |
|
50 |
} while (m != -1) |
|
51 |
} |
|
52 |
finally { stream.close } |
|
53 |
||
54 |
make_result(digest) |
|
55 |
} |
|
56 |
||
57 |
def digest(bytes: Array[Byte]): Digest = |
|
58 |
{ |
|
59 |
val digest = MessageDigest.getInstance("SHA") |
|
60 |
digest.update(bytes) |
|
61 |
||
62 |
make_result(digest) |
|
63 |
} |
|
64 |
||
54440 | 65 |
def digest(bytes: Bytes): Digest = bytes.sha1_digest |
66 |
||
67 |
def digest(string: String): Digest = digest(Bytes(string)) |
|
38473 | 68 |
} |
69 |