author | wenzelm |
Tue, 26 Mar 2024 21:44:18 +0100 | |
changeset 80018 | ac4412562c7b |
parent 78236 | f3a6140fa3b1 |
child 80359 | bb4e95d19ecb |
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 { |
77207
d98a99e4eea9
proper Shasum.digest, to emulate old form from build_history database;
wenzelm
parents:
77204
diff
changeset
|
17 |
/* digest */ |
d98a99e4eea9
proper Shasum.digest, to emulate old form from build_history database;
wenzelm
parents:
77204
diff
changeset
|
18 |
|
75393 | 19 |
final class Digest private[SHA1](rep: String) { |
75310 | 20 |
override def toString: String = rep |
55802
f7ceebe2f1b5
prefer abstract datatype -- in accordance to ML version;
wenzelm
parents:
54440
diff
changeset
|
21 |
override def hashCode: Int = rep.hashCode |
f7ceebe2f1b5
prefer abstract datatype -- in accordance to ML version;
wenzelm
parents:
54440
diff
changeset
|
22 |
override def equals(that: Any): Boolean = |
f7ceebe2f1b5
prefer abstract datatype -- in accordance to ML version;
wenzelm
parents:
54440
diff
changeset
|
23 |
that match { |
75310 | 24 |
case other: Digest => rep == other.toString |
55802
f7ceebe2f1b5
prefer abstract datatype -- in accordance to ML version;
wenzelm
parents:
54440
diff
changeset
|
25 |
case _ => false |
f7ceebe2f1b5
prefer abstract datatype -- in accordance to ML version;
wenzelm
parents:
54440
diff
changeset
|
26 |
} |
41954 | 27 |
} |
28 |
||
75309 | 29 |
def fake_digest(rep: String): Digest = new Digest(rep) |
38473 | 30 |
|
75393 | 31 |
def make_digest(body: MessageDigest => Unit): Digest = { |
75309 | 32 |
val digest_body = new Setup_Build.Digest_Body { def apply(sha: MessageDigest): Unit = body(sha)} |
33 |
new Digest(Setup_Build.make_digest(digest_body)) |
|
34 |
} |
|
62702 | 35 |
|
48409 | 36 |
def digest(file: JFile): Digest = |
75394 | 37 |
make_digest(sha => using(new FileInputStream(file)) { stream => |
38 |
val buf = new Array[Byte](65536) |
|
39 |
var m = 0 |
|
75709 | 40 |
while ({ |
75394 | 41 |
m = stream.read(buf, 0, buf.length) |
42 |
if (m != -1) sha.update(buf, 0, m) |
|
75709 | 43 |
m != -1 |
44 |
}) () |
|
75394 | 45 |
}) |
48343 | 46 |
|
62704
478b49f0d726
proper SHA1 digest as annex to heap file: Poly/ML reads precise segment length;
wenzelm
parents:
62702
diff
changeset
|
47 |
def digest(path: Path): Digest = digest(path.file) |
75309 | 48 |
def digest(bytes: Array[Byte]): Digest = make_digest(_.update(bytes)) |
54440 | 49 |
def digest(bytes: Bytes): Digest = bytes.sha1_digest |
50 |
def digest(string: String): Digest = digest(Bytes(string)) |
|
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
|
51 |
|
75310 | 52 |
val digest_length: Int = digest("").toString.length |
77204 | 53 |
|
77207
d98a99e4eea9
proper Shasum.digest, to emulate old form from build_history database;
wenzelm
parents:
77204
diff
changeset
|
54 |
|
d98a99e4eea9
proper Shasum.digest, to emulate old form from build_history database;
wenzelm
parents:
77204
diff
changeset
|
55 |
/* shasum */ |
d98a99e4eea9
proper Shasum.digest, to emulate old form from build_history database;
wenzelm
parents:
77204
diff
changeset
|
56 |
|
d98a99e4eea9
proper Shasum.digest, to emulate old form from build_history database;
wenzelm
parents:
77204
diff
changeset
|
57 |
final class Shasum private[SHA1](private[SHA1] val rep: List[String]) { |
d98a99e4eea9
proper Shasum.digest, to emulate old form from build_history database;
wenzelm
parents:
77204
diff
changeset
|
58 |
override def equals(other: Any): Boolean = |
d98a99e4eea9
proper Shasum.digest, to emulate old form from build_history database;
wenzelm
parents:
77204
diff
changeset
|
59 |
other match { |
d98a99e4eea9
proper Shasum.digest, to emulate old form from build_history database;
wenzelm
parents:
77204
diff
changeset
|
60 |
case that: Shasum => rep.equals(that.rep) |
d98a99e4eea9
proper Shasum.digest, to emulate old form from build_history database;
wenzelm
parents:
77204
diff
changeset
|
61 |
case _ => false |
d98a99e4eea9
proper Shasum.digest, to emulate old form from build_history database;
wenzelm
parents:
77204
diff
changeset
|
62 |
} |
d98a99e4eea9
proper Shasum.digest, to emulate old form from build_history database;
wenzelm
parents:
77204
diff
changeset
|
63 |
override def hashCode: Int = rep.hashCode |
d98a99e4eea9
proper Shasum.digest, to emulate old form from build_history database;
wenzelm
parents:
77204
diff
changeset
|
64 |
override def toString: String = Library.terminate_lines(rep) |
d98a99e4eea9
proper Shasum.digest, to emulate old form from build_history database;
wenzelm
parents:
77204
diff
changeset
|
65 |
|
d98a99e4eea9
proper Shasum.digest, to emulate old form from build_history database;
wenzelm
parents:
77204
diff
changeset
|
66 |
def is_empty: Boolean = rep.isEmpty |
d98a99e4eea9
proper Shasum.digest, to emulate old form from build_history database;
wenzelm
parents:
77204
diff
changeset
|
67 |
|
78236 | 68 |
def - (other: Shasum): Shasum = new Shasum(rep.filterNot(other.rep.toSet.contains)) |
69 |
||
77214
df8d71edbc79
clarified signature, using right-associative operation;
wenzelm
parents:
77213
diff
changeset
|
70 |
def :::(other: Shasum): Shasum = new Shasum(other.rep ::: rep) |
df8d71edbc79
clarified signature, using right-associative operation;
wenzelm
parents:
77213
diff
changeset
|
71 |
|
77675
9e5f8f6e58a0
more thorough treatment of build prefs, guarded by system option "build_through": avoid accidental rebuild of HOL etc.;
wenzelm
parents:
77215
diff
changeset
|
72 |
def filter(pred: String => Boolean): Shasum = new Shasum(rep.filter(pred)) |
9e5f8f6e58a0
more thorough treatment of build prefs, guarded by system option "build_through": avoid accidental rebuild of HOL etc.;
wenzelm
parents:
77215
diff
changeset
|
73 |
|
77207
d98a99e4eea9
proper Shasum.digest, to emulate old form from build_history database;
wenzelm
parents:
77204
diff
changeset
|
74 |
def digest: Digest = { |
d98a99e4eea9
proper Shasum.digest, to emulate old form from build_history database;
wenzelm
parents:
77204
diff
changeset
|
75 |
rep match { |
d98a99e4eea9
proper Shasum.digest, to emulate old form from build_history database;
wenzelm
parents:
77204
diff
changeset
|
76 |
case List(s) |
d98a99e4eea9
proper Shasum.digest, to emulate old form from build_history database;
wenzelm
parents:
77204
diff
changeset
|
77 |
if s.length == digest_length && s.forall(Symbol.is_ascii_hex) => fake_digest(s) |
d98a99e4eea9
proper Shasum.digest, to emulate old form from build_history database;
wenzelm
parents:
77204
diff
changeset
|
78 |
case _ => SHA1.digest(toString) |
d98a99e4eea9
proper Shasum.digest, to emulate old form from build_history database;
wenzelm
parents:
77204
diff
changeset
|
79 |
} |
d98a99e4eea9
proper Shasum.digest, to emulate old form from build_history database;
wenzelm
parents:
77204
diff
changeset
|
80 |
} |
d98a99e4eea9
proper Shasum.digest, to emulate old form from build_history database;
wenzelm
parents:
77204
diff
changeset
|
81 |
} |
d98a99e4eea9
proper Shasum.digest, to emulate old form from build_history database;
wenzelm
parents:
77204
diff
changeset
|
82 |
|
d98a99e4eea9
proper Shasum.digest, to emulate old form from build_history database;
wenzelm
parents:
77204
diff
changeset
|
83 |
val no_shasum: Shasum = new Shasum(Nil) |
d98a99e4eea9
proper Shasum.digest, to emulate old form from build_history database;
wenzelm
parents:
77204
diff
changeset
|
84 |
def flat_shasum(list: List[Shasum]): Shasum = new Shasum(list.flatMap(_.rep)) |
d98a99e4eea9
proper Shasum.digest, to emulate old form from build_history database;
wenzelm
parents:
77204
diff
changeset
|
85 |
def fake_shasum(text: String): Shasum = new Shasum(Library.trim_split_lines(text)) |
d98a99e4eea9
proper Shasum.digest, to emulate old form from build_history database;
wenzelm
parents:
77204
diff
changeset
|
86 |
|
77213 | 87 |
def shasum(digest: Digest, name: String): Shasum = |
88 |
new Shasum(List(digest.toString + " " + name)) |
|
89 |
def shasum_meta_info(digest: Digest): Shasum = |
|
90 |
shasum(digest, isabelle.setup.Build.META_INFO) |
|
77211 | 91 |
def shasum_sorted(args: List[(Digest, String)]): Shasum = |
77212 | 92 |
flat_shasum(args.sortBy(_._2).map(shasum)) |
38473 | 93 |
} |