src/Pure/General/sha1.scala
author wenzelm
Tue, 26 Mar 2024 21:44:18 +0100
changeset 80018 ac4412562c7b
parent 78236 f3a6140fa3b1
child 80359 bb4e95d19ecb
permissions -rw-r--r--
more robust XML body: allow empty text, as well as arbitrary pro-forma markup (e.g. see XML.blob in Isabelle/ML);
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
bd96f2a5beb0 digesting strings according to SHA-1 -- Scala version;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
bd96f2a5beb0 digesting strings according to SHA-1 -- Scala version;
wenzelm
parents:
diff changeset
     3
75309
216c2ac23a84 clarified signature;
wenzelm
parents: 75307
diff changeset
     4
SHA-1 message digest according to RFC 3174.
38473
bd96f2a5beb0 digesting strings according to SHA-1 -- Scala version;
wenzelm
parents:
diff changeset
     5
*/
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
package isabelle
bd96f2a5beb0 digesting strings according to SHA-1 -- Scala version;
wenzelm
parents:
diff changeset
     8
bd96f2a5beb0 digesting strings according to SHA-1 -- Scala version;
wenzelm
parents:
diff changeset
     9
48409
0d2114eb412a more explicit java.io.{File => JFile};
wenzelm
parents: 48343
diff changeset
    10
import java.io.{File => JFile, FileInputStream}
38473
bd96f2a5beb0 digesting strings according to SHA-1 -- Scala version;
wenzelm
parents:
diff changeset
    11
import java.security.MessageDigest
75309
216c2ac23a84 clarified signature;
wenzelm
parents: 75307
diff changeset
    12
216c2ac23a84 clarified signature;
wenzelm
parents: 75307
diff changeset
    13
import isabelle.setup.{Build => Setup_Build}
38473
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
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75382
diff changeset
    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
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75382
diff changeset
    19
  final class Digest private[SHA1](rep: String) {
75310
42baf7ffa088 tuned signature;
wenzelm
parents: 75309
diff changeset
    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
42baf7ffa088 tuned signature;
wenzelm
parents: 75309
diff changeset
    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
fb94df4505a0 explicit type SHA1.digest;
wenzelm
parents: 38473
diff changeset
    27
  }
fb94df4505a0 explicit type SHA1.digest;
wenzelm
parents: 38473
diff changeset
    28
75309
216c2ac23a84 clarified signature;
wenzelm
parents: 75307
diff changeset
    29
  def fake_digest(rep: String): Digest = new Digest(rep)
38473
bd96f2a5beb0 digesting strings according to SHA-1 -- Scala version;
wenzelm
parents:
diff changeset
    30
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75382
diff changeset
    31
  def make_digest(body: MessageDigest => Unit): Digest = {
75309
216c2ac23a84 clarified signature;
wenzelm
parents: 75307
diff changeset
    32
    val digest_body = new Setup_Build.Digest_Body { def apply(sha: MessageDigest): Unit = body(sha)}
216c2ac23a84 clarified signature;
wenzelm
parents: 75307
diff changeset
    33
    new Digest(Setup_Build.make_digest(digest_body))
216c2ac23a84 clarified signature;
wenzelm
parents: 75307
diff changeset
    34
  }
62702
e29f47e04180 tuned signature;
wenzelm
parents: 57638
diff changeset
    35
48409
0d2114eb412a more explicit java.io.{File => JFile};
wenzelm
parents: 48343
diff changeset
    36
  def digest(file: JFile): Digest =
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    37
    make_digest(sha => using(new FileInputStream(file)) { stream =>
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    38
      val buf = new Array[Byte](65536)
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    39
      var m = 0
75709
a068fb7346ef clarified while-loops;
wenzelm
parents: 75394
diff changeset
    40
      while ({
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    41
        m = stream.read(buf, 0, buf.length)
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    42
        if (m != -1) sha.update(buf, 0, m)
75709
a068fb7346ef clarified while-loops;
wenzelm
parents: 75394
diff changeset
    43
        m != -1
a068fb7346ef clarified while-loops;
wenzelm
parents: 75394
diff changeset
    44
      }) ()
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    45
    })
48343
3060e6343953 more SHA1.digest operations;
wenzelm
parents: 45673
diff changeset
    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
216c2ac23a84 clarified signature;
wenzelm
parents: 75307
diff changeset
    48
  def digest(bytes: Array[Byte]): Digest = make_digest(_.update(bytes))
54440
2c4940d2edf7 tuned signature;
wenzelm
parents: 50203
diff changeset
    49
  def digest(bytes: Bytes): Digest = bytes.sha1_digest
2c4940d2edf7 tuned signature;
wenzelm
parents: 50203
diff changeset
    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
42baf7ffa088 tuned signature;
wenzelm
parents: 75309
diff changeset
    52
  val digest_length: Int = digest("").toString.length
77204
d69732bc3dbe prefer explicit shasum;
wenzelm
parents: 75709
diff changeset
    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
f3a6140fa3b1 tuned signature: more operations;
wenzelm
parents: 77675
diff changeset
    68
    def - (other: Shasum): Shasum = new Shasum(rep.filterNot(other.rep.toSet.contains))
f3a6140fa3b1 tuned signature: more operations;
wenzelm
parents: 77675
diff changeset
    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
05a4ce3f6b0c tuned whitespace;
wenzelm
parents: 77212
diff changeset
    87
  def shasum(digest: Digest, name: String): Shasum =
05a4ce3f6b0c tuned whitespace;
wenzelm
parents: 77212
diff changeset
    88
    new Shasum(List(digest.toString + " " + name))
05a4ce3f6b0c tuned whitespace;
wenzelm
parents: 77212
diff changeset
    89
  def shasum_meta_info(digest: Digest): Shasum =
05a4ce3f6b0c tuned whitespace;
wenzelm
parents: 77212
diff changeset
    90
    shasum(digest, isabelle.setup.Build.META_INFO)
77211
a917f580a107 clarified signature;
wenzelm
parents: 77209
diff changeset
    91
  def shasum_sorted(args: List[(Digest, String)]): Shasum =
77212
a7c4510ae251 tuned --- implicit split;
wenzelm
parents: 77211
diff changeset
    92
    flat_shasum(args.sortBy(_._2).map(shasum))
38473
bd96f2a5beb0 digesting strings according to SHA-1 -- Scala version;
wenzelm
parents:
diff changeset
    93
}