package MlFront_Core

  1. Overview
  2. Docs

Source file Squish.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
(** 8 characters is the minimum length of the SHA256 hex chars.
    Up to 4 characters may be used as either the prefix, suffix
    or both prefix and suffix of the full library name *)
let fixlen = 12

let firstchars_len = 2
let lastchars_len = 2
let sanitize s = String.map (function '.' -> '_' | c -> c) s

let fixlen_name name =
  match String.length name with
  | 0 -> failwith "Illegal state. MlFront validated names can't be empty"
  | fulllen when fulllen <= firstchars_len + lastchars_len ->
      (* The entire library can be visible *)
      let d = Digestif.SHA256.digest_string name in
      let hex_d = Digestif.SHA256.to_hex d in
      (* [id] must make room for [len(library)] *)
      let id = String.sub hex_d 0 (fixlen - fulllen) in
      name ^ id
  | fulllen ->
      (* Just the sanitized form of the first and last characters will be visible *)
      let firstchars = String.sub name 0 firstchars_len |> sanitize in
      let lastchars =
        String.sub name (fulllen - lastchars_len) lastchars_len |> sanitize
      in
      (* The digest will be the full string. That includes the first and last
         characters since sanitization can make those characters not unique. *)
      let d = Digestif.SHA256.digest_string name in
      let hex_d = Digestif.SHA256.to_hex d in
      (* [id] must make room for len(firstchars) and len(lastchars) *)
      let id =
        String.sub hex_d 0
          (fixlen - String.length firstchars - String.length lastchars)
      in
      firstchars ^ id ^ lastchars

(* Test the use of the first and last two characters of name. *)
let () = assert ("Sodec27f3eQt" = fixlen_name "SonicScout_Setup.Qt")

(* Test when last two characters includes a dot. *)
let () = assert ("Sodb5fffcf_Q" = fixlen_name "SonicScout_Setup.Q")