author | wenzelm |
Mon, 17 Oct 2022 12:15:23 +0200 | |
changeset 76322 | 43e66527fa93 |
parent 75709 | a068fb7346ef |
child 77204 | d69732bc3dbe |
permissions | -rw-r--r-- |
38473 | 1 |
/* Title: Pure/General/sha1.scala |
2 |
Author: Makarius |
|
3 |
||
75309 | 4 |
SHA-1 message digest according to RFC 3174. |
38473 | 5 |
*/ |
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
48409 | 10 |
import java.io.{File => JFile, FileInputStream} |
38473 | 11 |
import java.security.MessageDigest |
75309 | 12 |
|
13 |
import isabelle.setup.{Build => Setup_Build} |
|
38473 | 14 |
|
15 |
||
75393 | 16 |
object SHA1 { |
17 |
final class Digest private[SHA1](rep: String) { |
|
75310 | 18 |
override def toString: String = rep |
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 { |
75310 | 22 |
case other: Digest => rep == other.toString |
55802
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 |
} |
75348 | 25 |
def shasum(name: String): String = rep + " " + name |
41954 | 26 |
} |
27 |
||
75309 | 28 |
def fake_digest(rep: String): Digest = new Digest(rep) |
38473 | 29 |
|
75393 | 30 |
def make_digest(body: MessageDigest => Unit): Digest = { |
75309 | 31 |
val digest_body = new Setup_Build.Digest_Body { def apply(sha: MessageDigest): Unit = body(sha)} |
32 |
new Digest(Setup_Build.make_digest(digest_body)) |
|
33 |
} |
|
62702 | 34 |
|
48409 | 35 |
def digest(file: JFile): Digest = |
75394 | 36 |
make_digest(sha => using(new FileInputStream(file)) { stream => |
37 |
val buf = new Array[Byte](65536) |
|
38 |
var m = 0 |
|
75709 | 39 |
while ({ |
75394 | 40 |
m = stream.read(buf, 0, buf.length) |
41 |
if (m != -1) sha.update(buf, 0, m) |
|
75709 | 42 |
m != -1 |
43 |
}) () |
|
75394 | 44 |
}) |
48343 | 45 |
|
62704
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62702
diff
changeset
|
46 |
def digest(path: Path): Digest = digest(path.file) |
75309 | 47 |
def digest(bytes: Array[Byte]): Digest = make_digest(_.update(bytes)) |
54440 | 48 |
def digest(bytes: Bytes): Digest = bytes.sha1_digest |
49 |
def digest(string: String): Digest = digest(Bytes(string)) |
|
72654 | 50 |
def digest_set(digests: List[Digest]): Digest = |
51 |
digest(cat_lines(digests.map(_.toString).sorted)) |
|
57638
ed58e740a699
less authentic SHA1.digest: trust Scala side on blobs and avoid re-calculation via Foreign Language Interface, which might be a cause of problems;
wenzelm
parents:
55802
diff
changeset
|
52 |
|
75310 | 53 |
val digest_length: Int = digest("").toString.length |
38473 | 54 |
} |