From e64fccd367ba690d2a588760dbe085688e994365 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Wed, 24 Feb 2021 12:33:32 +0000 Subject: [PATCH] better document and position the hash base64 encoding (#234) We now document why we use a custom base64 charset. The old "b64" name was also too generic, so it might have been misused for other purposes. --- hash.go | 14 +++++++++++++- main.go | 2 -- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/hash.go b/hash.go index 729328a..673553c 100644 --- a/hash.go +++ b/hash.go @@ -141,6 +141,16 @@ func buildidOf(path string) (string, error) { return string(out), nil } +var ( + // Hashed names are base64-encoded. + // Go names can only be letters, numbers, and underscores. + // This means we can use base64's URL encoding, minus '-'. + // Use the URL encoding, replacing '-' with a duplicate 'z'. + // Such a lossy encoding is fine, since we never decode hashes. + nameCharset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_z" + nameBase64 = base64.NewEncoding(nameCharset) +) + func hashWith(salt []byte, name string) string { if len(salt) == 0 { panic("hashWith: empty salt") @@ -154,8 +164,10 @@ func hashWith(salt []byte, name string) string { d.Write(salt) d.Write(opts.Seed) io.WriteString(d, name) - sum := b64.EncodeToString(d.Sum(nil)) + sum := nameBase64.EncodeToString(d.Sum(nil)) + // TODO: Just make the first letter uppercase or lowercase as needed. + // This is also not needed for non-names, like import paths. if token.IsExported(name) { return "Z" + sum[:length] } diff --git a/main.go b/main.go index f1131c1..5c93bae 100644 --- a/main.go +++ b/main.go @@ -87,8 +87,6 @@ var ( fset = token.NewFileSet() sharedTempDir = os.Getenv("GARBLE_SHARED") - nameCharset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_z" - b64 = base64.NewEncoding(nameCharset) printConfig = printer.Config{Mode: printer.RawFormat} // origImporter is a go/types importer which uses the original versions