cid.gno
5.90 Kb · 146 lines
1package media
2
3import (
4 "encoding/hex"
5 "strconv"
6)
7
8// CIDv1 grammar (design §12.1; frontend chain/cid.ts is the twin):
9// multibase 'b' (RFC 4648 base32, lower case, no padding) over exactly
10// 0x01 · codec · 0x12 · 0x20 · digest[32], codec 0x55 (raw) or 0x70
11// (dag-pb). That is 36 bytes = 58 base32 characters + 'b'. The unused two
12// trailing bits must be zero, so every CID has exactly one spelling. CIDv0
13// ("Qm…"), upper case, other bases, codecs or hash functions are refused.
14
15const (
16 b32Alphabet = "abcdefghijklmnopqrstuvwxyz234567"
17 cidBytes = 36
18 cidChars = 58 // ceil(36*8/5)
19 mhSha256 = 0x12
20 digestLen = 32
21)
22
23// ParseCIDv1 returns the codec (CodecRaw or CodecDagPB) and the sha2-256
24// digest as lower-case hex.
25func ParseCIDv1(s string) (codec int, digestHex string, err error) {
26 if s == "" {
27 return 0, "", mediaErr("cid: empty")
28 }
29 if len(s) >= 2 && s[0] == 'Q' && s[1] == 'm' {
30 return 0, "", mediaErr("cid: CIDv0 is not accepted; convert it to CIDv1")
31 }
32 if s[0] != 'b' {
33 return 0, "", mediaErr("cid: multibase must be 'b' (base32 lower case)")
34 }
35 if len(s) != cidChars+1 {
36 return 0, "", mediaErr("cid: expected " + strconv.Itoa(cidChars+1) + " characters, got " + strconv.Itoa(len(s)))
37 }
38 bz, derr := base32Decode(s[1:])
39 if derr != nil {
40 return 0, "", derr
41 }
42 if len(bz) != cidBytes {
43 return 0, "", mediaErr("cid: wrong length")
44 }
45 if bz[0] != 1 {
46 return 0, "", mediaErr("cid: version must be 1")
47 }
48 c := int(bz[1])
49 if c != CodecRaw && c != CodecDagPB {
50 return 0, "", mediaErr("cid: codec must be raw (0x55) or dag-pb (0x70)")
51 }
52 if bz[2] != mhSha256 || bz[3] != digestLen {
53 return 0, "", mediaErr("cid: multihash must be sha2-256")
54 }
55 return c, hex.EncodeToString(bz[4:]), nil
56}
57
58// CIDFromDigest is the inverse of ParseCIDv1. It panics on an unsupported
59// codec or a digest that is not 64 lower-case hex characters.
60func CIDFromDigest(codec int, digestHex string) string {
61 if codec != CodecRaw && codec != CodecDagPB {
62 panic("media: cid: unsupported codec")
63 }
64 d, ok := Raw32(digestHex)
65 if !ok {
66 panic("media: cid: digest must be 64 lower-case hex characters")
67 }
68 bz := make([]byte, 0, cidBytes)
69 bz = append(bz, 1, byte(codec), mhSha256, digestLen)
70 bz = append(bz, d...)
71 return "b" + base32Encode(bz)
72}
73
74// IsHex32 reports whether s is exactly 64 lower-case hex characters.
75func IsHex32(s string) bool {
76 _, ok := Raw32(s)
77 return ok
78}
79
80// Raw32 decodes exactly 64 lower-case hex characters into 32 raw bytes
81// (one pass, table lookups: VM loops cost ~5k gas per character).
82func Raw32(s string) (string, bool) {
83 if len(s) != 64 {
84 return "", false
85 }
86 out := make([]byte, 32)
87 for i := 0; i < 32; i++ {
88 hi, lo := hexRev[s[2*i]], hexRev[s[2*i+1]]
89 if hi > 15 || lo > 15 {
90 return "", false
91 }
92 out[i] = hi<<4 | lo
93 }
94 return string(out), true
95}
96
97// hexRev and b32Rev map a byte to its digit value, 0xff when it is not a
98// lower-case hex / base32 digit.
99const (
100 hexRev = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x0a\x0b\x0c\x0d\x0e\x0f\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
101 b32Rev = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x1a\x1b\x1c\x1d\x1e\x1f\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
102)
103
104func base32Encode(bz []byte) string {
105 out := make([]byte, 0, (len(bz)*8+4)/5)
106 var buf uint32
107 bits := uint(0)
108 for _, b := range bz {
109 buf = buf<<8 | uint32(b)
110 bits += 8
111 for bits >= 5 {
112 bits -= 5
113 out = append(out, b32Alphabet[(buf>>bits)&31])
114 }
115 buf &= (1 << bits) - 1
116 }
117 if bits > 0 {
118 out = append(out, b32Alphabet[(buf<<(5-bits))&31])
119 }
120 return string(out)
121}
122
123// base32Decode is strict: lower-case alphabet only, and the unused
124// trailing bits must be zero.
125func base32Decode(s string) ([]byte, error) {
126 out := make([]byte, 0, len(s)*5/8)
127 var buf uint32
128 bits := uint(0)
129 for i := 0; i < len(s); i++ {
130 v := b32Rev[s[i]]
131 if v > 31 {
132 return nil, mediaErr("cid: invalid base32 character " + strconv.Quote(s[i:i+1]))
133 }
134 buf = buf<<5 | uint32(v)
135 bits += 5
136 if bits >= 8 {
137 bits -= 8
138 out = append(out, byte(buf>>bits))
139 buf &= (1 << bits) - 1
140 }
141 }
142 if bits >= 5 || buf != 0 {
143 return nil, mediaErr("cid: non-canonical base32 padding bits")
144 }
145 return out, nil
146}