GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — locks/id.go at master · asciimoth/locks

Expand file tree

/

id.go

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
82 lines (74 loc) · 2.21 KB
/

id.go

Copy path

File metadata and controls

82 lines (74 loc) · 2.21 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package locks
import (
"crypto/sha256"
"encoding/base32"
"encoding/base64"
"fmt"
"strings"
"golang.org/x/crypto/blake2b"
)
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
var b32 = base32.StdEncoding.WithPadding(base32.NoPadding)
// ID is a canonical digest and algorithm name. Use ParseID to validate external IDs.
type ID string
// ParseID rejects aliases, padding, invalid pad bits and known algorithm length errors.
func ParseID(s string) (ID, error) {
digest, alg, ok := strings.Cut(s, ":")
if !ok || !validName(alg) || len(digest) < 52 || len(digest) > 103 {
return "", fmt.Errorf("%w: identifier", ErrFormat)
}
b, e := b32.DecodeString(digest)
if e != nil || len(b) < 32 || len(b) > 64 || b32.EncodeToString(b) != digest {
return "", fmt.Errorf("%w: digest", ErrFormat)
}
if (alg == "blake2" || alg == "sha256") && len(b) != 32 {
return "", fmt.Errorf("%w: digest length", ErrFormat)
}
return ID(s), nil
}
// Hash hashes the exact raw bytes. An empty algorithm selects blake2.
func Hash(raw []byte, algorithm string) (ID, error) {
var sum [32]byte
switch algorithm {
case "", "blake2":
algorithm = "blake2"
sum = blake2b.Sum256(raw)
case "sha256":
sum = sha256.Sum256(raw)
default:
return "", fmt.Errorf("%w: algorithm %q", ErrUnsupported, algorithm)
}
return ID(b32.EncodeToString(sum[:]) + ":" + algorithm), nil
}
// ParseKey resolves an HTTP key token. Raw keys are never interpreted as paths or URLs.
func ParseKey(token string, limits Limits) (ID, error) {
l, e := limits.normalized()
if e != nil {
return "", e
}
if strings.HasPrefix(token, "id.") {
return ParseID(token[3:])
}
if !strings.HasPrefix(token, "raw.") {
return "", fmt.Errorf("%w: key token", ErrFormat)
}
alg, enc, ok := strings.Cut(token[4:], ".")
if !ok || !validName(alg) {
return "", ErrFormat
}
if len(enc) > base64.RawURLEncoding.EncodedLen(l.RawKeyBytes) {
return "", ErrLimit
}
b, e := base64.RawURLEncoding.Strict().DecodeString(enc)
if e != nil || base64.RawURLEncoding.EncodeToString(b) != enc {
return "", ErrFormat
}
return Hash(b, alg)
}
func prefix(id ID) string { return string(id)[:52] }
func component(p string, depth int) string {
if depth == 0 {
return p[:2]
}
return p[depth : depth+1]
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.