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