diff --git a/go.mod b/go.mod index 2edb93bc..43ea46d4 100644 --- a/go.mod +++ b/go.mod @@ -19,7 +19,6 @@ require ( github.com/go-chi/traceid v0.2.0 github.com/go-chi/transport v0.1.0 github.com/goware/rerun v0.0.9 - github.com/jxskiss/base62 v1.1.0 github.com/lestrrat-go/jwx/v2 v2.0.20 github.com/mdlayher/vsock v1.2.1 github.com/rs/zerolog v1.32.0 diff --git a/go.sum b/go.sum index e674f7b6..de31f849 100644 --- a/go.sum +++ b/go.sum @@ -240,8 +240,6 @@ github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/ github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= -github.com/jxskiss/base62 v1.1.0 h1:A5zbF8v8WXx2xixnAKD2w+abC+sIzYJX+nxmhA6HWFw= -github.com/jxskiss/base62 v1.1.0/go.mod h1:HhWAlUXvxKThfOlZbcuFzsqwtF5TcqS9ru3y5GfjWAc= github.com/kevinburke/ssh_config v0.0.0-20180830205328-81db2a75821e/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM= github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM= github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4= diff --git a/rpc/helpers_test.go b/rpc/helpers_test.go index 16106602..00dac65c 100644 --- a/rpc/helpers_test.go +++ b/rpc/helpers_test.go @@ -5,7 +5,6 @@ import ( "crypto/rand" "crypto/rsa" "encoding/base64" - "encoding/binary" "encoding/json" "fmt" "io" @@ -33,7 +32,6 @@ import ( dynamodbtypes "github.com/aws/aws-sdk-go-v2/service/dynamodb/types" "github.com/aws/aws-sdk-go-v2/service/kms" kmstypes "github.com/aws/aws-sdk-go-v2/service/kms/types" - "github.com/jxskiss/base62" "github.com/lestrrat-go/jwx/v2/jwa" "github.com/lestrrat-go/jwx/v2/jwk" "github.com/lestrrat-go/jwx/v2/jwt" @@ -562,13 +560,6 @@ func newSession(t *testing.T, enc *enclave.Enclave, issuer string, signingSessio return newSessionFromData(t, enc, payload) } -func newRandAccessKey(projectID uint64) string { - buf := make([]byte, 24) - binary.BigEndian.PutUint64(buf, projectID) - rand.Read(buf[8:]) - return base62.EncodeToString(buf) -} - type walletServiceMock struct { registeredUsers map[string]struct{} registeredSessions map[string]struct{} diff --git a/rpc/send_transaction_test.go b/rpc/send_transaction_test.go index 4ab9b5d8..e5387567 100644 --- a/rpc/send_transaction_test.go +++ b/rpc/send_transaction_test.go @@ -8,6 +8,7 @@ import ( mathrand "math/rand" "net/http" "net/http/httptest" + "strconv" "testing" "github.com/0xsequence/ethkit/ethwallet" @@ -68,7 +69,7 @@ func TestRPC_SendIntent_SendTransaction(t *testing.T) { c := proto.NewWaasAuthenticatorClient(srv.URL, http.DefaultClient) header := make(http.Header) - header.Set("X-Access-Key", newRandAccessKey(tenant.ProjectID)) + header.Set("X-Sequence-Project", strconv.Itoa(int(tenant.ProjectID))) ctx, err := proto.WithHTTPRequestHeaders(context.Background(), header) res, err := c.SendIntent(ctx, intent) diff --git a/rpc/sessions_test.go b/rpc/sessions_test.go index 72442292..db788ec6 100644 --- a/rpc/sessions_test.go +++ b/rpc/sessions_test.go @@ -8,6 +8,7 @@ import ( mathrand "math/rand" "net/http" "net/http/httptest" + "strconv" "strings" "testing" "time" @@ -200,7 +201,7 @@ func TestRPC_RegisterSession(t *testing.T) { c := proto.NewWaasAuthenticatorClient(srv.URL, http.DefaultClient) header := make(http.Header) - header.Set("X-Access-Key", newRandAccessKey(tenant.ProjectID)) + header.Set("X-Sequence-Project", strconv.Itoa(int(tenant.ProjectID))) ctx, err := proto.WithHTTPRequestHeaders(context.Background(), header) require.NoError(t, err) @@ -337,7 +338,7 @@ func TestRPC_SendIntent_DropSession(t *testing.T) { c := proto.NewWaasAuthenticatorClient(srv.URL, http.DefaultClient) header := make(http.Header) - header.Set("X-Access-Key", newRandAccessKey(tenant.ProjectID)) + header.Set("X-Sequence-Project", strconv.Itoa(int(tenant.ProjectID))) ctx, err := proto.WithHTTPRequestHeaders(context.Background(), header) res, err := c.SendIntent(ctx, intent) @@ -411,7 +412,7 @@ func TestRPC_SendIntent_ListSessions(t *testing.T) { c := proto.NewWaasAuthenticatorClient(srv.URL, http.DefaultClient) header := make(http.Header) - header.Set("X-Access-Key", newRandAccessKey(tenant.ProjectID)) + header.Set("X-Sequence-Project", strconv.Itoa(int(tenant.ProjectID))) ctx, err := proto.WithHTTPRequestHeaders(context.Background(), header) require.NoError(t, err) diff --git a/rpc/sign_message_test.go b/rpc/sign_message_test.go index 2d4e436f..1585d056 100644 --- a/rpc/sign_message_test.go +++ b/rpc/sign_message_test.go @@ -7,6 +7,7 @@ import ( mathrand "math/rand" "net/http" "net/http/httptest" + "strconv" "testing" "github.com/0xsequence/ethkit/ethwallet" @@ -66,7 +67,7 @@ func TestRPC_SendIntent_SignMessage(t *testing.T) { c := proto.NewWaasAuthenticatorClient(srv.URL, http.DefaultClient) header := make(http.Header) - header.Set("X-Access-Key", newRandAccessKey(tenant.ProjectID)) + header.Set("X-Sequence-Project", strconv.Itoa(int(tenant.ProjectID))) ctx, err := proto.WithHTTPRequestHeaders(context.Background(), header) res, err := c.SendIntent(ctx, intent) diff --git a/rpc/tenant/middleware.go b/rpc/tenant/middleware.go index 4ebac47a..b8f6f367 100644 --- a/rpc/tenant/middleware.go +++ b/rpc/tenant/middleware.go @@ -2,15 +2,15 @@ package tenant import ( "context" - "encoding/binary" "fmt" "net/http" "slices" + "strconv" + "strings" "github.com/0xsequence/waas-authenticator/data" "github.com/0xsequence/waas-authenticator/proto" "github.com/0xsequence/waas-authenticator/rpc/crypto" - "github.com/jxskiss/base62" ) // Middleware validates that the tenant sent in X-Access-Key header is valid and stores it in context. @@ -19,21 +19,27 @@ func Middleware(tenants *data.TenantTable, tenantKeys []string) func(http.Handle return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ctx := r.Context() - // Get projectID based on access key header which is encoded in the value - // and place the access key on the context. + // Place the access key in context as it's used by services downstream accessKey := r.Header.Get("x-access-key") if accessKey != "" { ctx = WithAccessKey(ctx, accessKey) } - projectID, err := decodeProjectIDFromAccessKey(accessKey) + // Get projectID from the header populated by the ingress service + projectHeader := r.Header.Get("x-sequence-project") + if projectHeader == "" { + proto.RespondWithError(w, fmt.Errorf("missing X-Sequence-Project header")) + return + } + + projectID, err := strconv.Atoi(strings.TrimSpace(projectHeader)) if err != nil { - proto.RespondWithError(w, fmt.Errorf("invalid tenant: %v", projectID)) + proto.RespondWithError(w, fmt.Errorf("parse project ID: %w", err)) return } // Find tenant based on project id - tenant, found, err := tenants.GetLatest(ctx, projectID) + tenant, found, err := tenants.GetLatest(ctx, uint64(projectID)) if err != nil { proto.RespondWithError(w, fmt.Errorf("could not retrieve tenant: %w", err)) return @@ -62,11 +68,3 @@ func Middleware(tenants *data.TenantTable, tenantKeys []string) func(http.Handle }) } } - -func decodeProjectIDFromAccessKey(accessKey string) (uint64, error) { - buf, err := base62.DecodeString(accessKey) - if err != nil || len(buf) < 8 { - return 0, fmt.Errorf("invalid access key") - } - return binary.BigEndian.Uint64(buf[:8]), nil -} diff --git a/vendor/github.com/jxskiss/base62/.gitignore b/vendor/github.com/jxskiss/base62/.gitignore deleted file mode 100644 index 9f11b755..00000000 --- a/vendor/github.com/jxskiss/base62/.gitignore +++ /dev/null @@ -1 +0,0 @@ -.idea/ diff --git a/vendor/github.com/jxskiss/base62/LICENSE b/vendor/github.com/jxskiss/base62/LICENSE deleted file mode 100644 index 2efdaa0f..00000000 --- a/vendor/github.com/jxskiss/base62/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) 2019 Shawn Wang - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/vendor/github.com/jxskiss/base62/README.md b/vendor/github.com/jxskiss/base62/README.md deleted file mode 100644 index 51925c19..00000000 --- a/vendor/github.com/jxskiss/base62/README.md +++ /dev/null @@ -1,191 +0,0 @@ -# base62 - -[![GoDoc](https://img.shields.io/badge/api-Godoc-blue.svg)][godoc] -[![Go Report Card](https://goreportcard.com/badge/github.com/jxskiss/base62)][goreport] -[![Issues](https://img.shields.io/github/issues/jxskiss/base62.svg)][issues] -[![GitHub release](http://img.shields.io/github/release/jxskiss/base62.svg)][release] -[![MIT License](http://img.shields.io/badge/license-MIT-blue.svg)][license] - -[godoc]: https://pkg.go.dev/github.com/jxskiss/base62 -[goreport]: https://goreportcard.com/report/github.com/jxskiss/base62 -[issues]: https://github.com/jxskiss/base62/issues -[release]: https://github.com/jxskiss/base62/releases -[license]: https://github.com/jxskiss/base62/blob/master/LICENSE - -base62 is a correctly implemented, compact and fast implementation of `Base62` encoding/decoding algorithm. -It is inspired by the [java implementation by glowfall](https://github.com/glowfall/base62). - -This `Base62` implementation can encode/decode both integers and bytes of arbitrary length, -the correctness is tested using a large number of randomly generated bytes. - -It is much faster than `big.Int` based implementation and is not much slower than typical `Base64` -implementations. See the benchmark results below. - -## Why Base62 - -In comparison with Base64/Base32, Base62 is more friendly to human: - -- it contains only alpha-numerical symbols, no special characters -- can be validated by eyes and more simple regexp -- can be fully selected by mouse double-click in any text editors and browser address bar -- it's compact and generates shorter strings than Base32 -- it's the most natural and unambiguous way to encode your data in human-readable form :) - -Variations of Base62 algorithm cann be widely used to represent authentication data in printable and -easy-copyable form, for example to encode OAuth 2.0 `access_token` data. - -## Usage - -```go -// Basic usage. -Encode(src []byte) []byte -EncodeToString(src []byte) string -Decode(src []byte) ([]byte, error) -DecodeString(src string) ([]byte, error) -FormatInt(num int64) []byte -FormatUint(num uint64) []byte -ParseInt(src []byte) (int64, error) -ParseUint(src []byte) (uint64, error) - -// Providing a dst buffer, you may reuse buffers to reduce memory allocation. -EncodeToBuf(dst []byte, src []byte) []byte -DecodeToBuf(dst []byte, src []byte) ([]byte, error) -AppendInt(dst []byte, num int64) []byte -AppendUint(dst []byte, num uint64) []byte - -// Or you may use a custom encoding alphabet. -enc := NewEncoding("...my-62-byte-string-alphabet...") -enc.XXX() -``` - -## Benchmark - -```text -Benchmark_Encode-12 11654754 97.41 ns/op -Benchmark_Decode-12 15481666 73.60 ns/op -Benchmark_EncodeToString-12 11950086 99.54 ns/op -Benchmark_DecodeString-12 16301325 74.36 ns/op - -Benchmark_EncodeToBuf-12 13855840 84.68 ns/op -Benchmark_DecodeToBuf-12 97695962 12.21 ns/op - -Benchmark_EncodeInteger-12 29119437 41.30 ns/op -Benchmark_DecodeInteger-12 120328183 9.917 ns/op - -Benchmark_Encode_BigInt-12 1000000 1048 ns/op - -Benchmark_Base64_EncodeToString-12 17803440 70.12 ns/op -Benchmark_Base64_DecodeString-12 19884616 55.09 ns/op - -Benchmark_Base64_Encode-12 68163142 17.93 ns/op -Benchmark_Base64_Decode-12 41990004 28.25 ns/op -``` - -## How it works - -Encoding Base64 and Base32 both are bit-aligned, 6 bits for Base64 (2^6 = 64) -and 5 bits for Base32 (2^5 = 32), but Base62 is not bit-aligned, it's inefficient -to do divmod operation for non-bit-aligned integers, typical Base62 implementations -are BigInt based, which encodes input data block by block to get better performance, -(e.g. https://github.com/keybase/saltpack/blob/master/encoding/basex). - -64 characters can fully represent 6 bits, but 62 characters can not, if each character -represents 5 bits, that is the Base32 encoding. - -A naive BigInt based algorithm gives the shortest result, which is roughly like this -(e.g. https://github.com/eknkc/basex/blob/6baac8ea8b19cc66d125286d213770fec0691867/basex.go#L46): - -```go -digits := []int{0} -for i := 0; i < len(src); i++ { - carry := int(src[i]) - - for j := 0; j < len(digits); j++ { - carry += digits[j] << 8 - digits[j] = carry % e.base - carry = carry / e.base - } - - for carry > 0 { - digits = append(digits, carry%e.base) - carry = carry / e.base - } -} -// map to encoding alphabet -_ = digits -``` - -This works but the time complexity is O(n^2) where n is the length of src. If the -input can be very large, the cost is unacceptable. - -Improved algorithm splits the input into blocks, which reduce the time complexity to -O(kn) where k is the block size, and n is the length of src, it generates slightly -longer result than naive BigInt algorithm, but it is worth for the reduced time -complexity. When k is n, it degrades to the naive algorithm, when k is 1, the output -length is twice of the input, we don't want that. 32 is chosen as the block size -in library [saltpack/encoding/basex](https://github.com/keybase/saltpack/blob/master/encoding/basex). - -Inspired by the [java implementation by glowfall](https://github.com/glowfall/base62), -this library is not BigInt based, it encodes and decodes any arbitrary bytes in O(n) -time complexity. Here is a brief description of the algorithm. - -This library uses a variadic length encoding, for each 6 bits, if the value is in range -[0, 62), it can be directly map to the 62 characters, if the value is 62 or 63 which -exceeds 61, we turn to encoding just the lower 5 bits. If we find a way to recognize -the 5 bits pattern, then we can correctly decode it back to the source data. - -The binary representation of 62 and 63 is: - - 62 - 0b_0011_1110 - 63 - 0b_0011_1111 - -They have a common mask `0b_0011_1110`, in range [0, 62), there are another two integers -have a similar mask `0b_0001_1110`, 30 and 31, which is: - - 30 - 0b_0001_1110 - 31 - 0b_0001_1111 - -The four integers 30, 31, 62, 63 share a common mask `0b_0001_1110`, while all other -integers in range [0, 64) don't share the mask, i.e. for all other integers, the -expression `value & 0b_0001_1110 == 0b_0001_1110` evaluates to false. - -**This is the key point!** - -We define a `compactMask` as `0b_0001_1110`. - -When encoding, for each 6 bits integer x, if `x & compactMask == compactMask` is true, -it must be one of 30, 31, 62 or 63, we just encode the lower 5 bits, which are -`0b_0001_1110` (30) and `0b_0001_1111` (31), we leave the 6-th bit to next byte. - -When decoding, for each encoded byte x, we check `x & compactMask == compactMask`, when -it is false, we know that the byte represents 6 bits in the raw data, it is in range -[0, 30) or [32, 62), else it represents 5 bits in the raw data, it is 30 or 31. - -That is it, by using variadic length encoding, we successfully limit the value range -to [0, 62), we get very compact result and a simple O(n) time complexity for encoding -and decoding data of arbitrary length. - -## Compatibility - -This library guarantees that it can correctly decode data encoded by itself. - -The encoded result is not compatible with BigInt based algorithm, -(e.g. [saltpack/encoding/basex, GMP and GnuPG](https://github.com/jxskiss/base62/issues/2)). - -The algorithm may be ported to other languages in future (if someone does a porting, -I'm glad to link it here :-) ). - -## Changelog - -### v1.1.0 - 2022/1/3 - -1. Refactor the encoding code to be simpler and cleaner, as a bonus, it gives better - performance which is 1.5X~ faster than the old. -2. Add a brief description of the algorithm and state the compatibility with other - Base62 implementations. - -### v1.0.0 - 2021/10/23 - -First stable release, the package has been used in several small and medium projects. - -This release adds new APIs which help to reuse buffers to reduce memory allocation. diff --git a/vendor/github.com/jxskiss/base62/base62.go b/vendor/github.com/jxskiss/base62/base62.go deleted file mode 100644 index f22decdf..00000000 --- a/vendor/github.com/jxskiss/base62/base62.go +++ /dev/null @@ -1,280 +0,0 @@ -package base62 - -import ( - "math/bits" - "reflect" - "strconv" - "unsafe" -) - -const ( - base = 62 - compactMask = 0x1E // 00011110 - mask5bits = 0x1F // 00011111 - mask6bits = 0x3F // 00111111 -) - -// An Encoding is a radix 62 encoding/decoding scheme, defined by a -// 62-character alphabet. -type Encoding struct { - encode [base]byte - decodeMap [256]byte -} - -// NewEncoding returns a new Encoding defined by the given alphabet, -// which must be a 62-byte string that does not contain CR / LF ('\r', '\n'). -func NewEncoding(encoder string) *Encoding { - if len(encoder) != base { - panic("encoding alphabet is not 62-bytes long") - } - for i := 0; i < len(encoder); i++ { - if encoder[i] == '\n' || encoder[i] == '\r' { - panic("encoding alphabet contains newline character") - } - } - - e := new(Encoding) - copy(e.encode[:], encoder) - for i := 0; i < len(e.decodeMap); i++ { - e.decodeMap[i] = 0xFF - } - for i := 0; i < len(encoder); i++ { - e.decodeMap[encoder[i]] = byte(i) - } - return e -} - -// Encode encodes src using the encoding enc, returns the encoded bytes. -func (enc *Encoding) Encode(src []byte) []byte { - return enc._encodeV2(src) -} - -// _encodeV1 is obsolete, it is here to make sure that the new -// implementation is compatible with the old one. -// -// We don't want to break the programs which use this package. -func (enc *Encoding) _encodeV1(src []byte) []byte { - if len(src) == 0 { - return []byte{} - } - dst := make([]byte, 0, len(src)*9/5) - encoder := newEncoder(src) - return encoder.encode(dst, enc.encode[:]) -} - -func (enc *Encoding) _encodeV2(src []byte) []byte { - if len(src) == 0 { - return []byte{} - } - dst := make([]byte, 0, len(src)*9/5) - encoder := newEncoder(src) - return encoder.encodeV2(dst, enc.encode[:]) -} - -// EncodeToString returns a base62 string representation of src. -func (enc *Encoding) EncodeToString(src []byte) string { - ret := enc.Encode(src) - return b2s(ret) -} - -// EncodeToBuf encodes src using the encoding enc, appending the encoded -// bytes to dst. If dst has not enough capacity, it copies dst and returns -// the extended buffer. -func (enc *Encoding) EncodeToBuf(dst []byte, src []byte) []byte { - if len(src) == 0 { - return []byte{} - } - encoder := newEncoder(src) - return encoder.encodeV2(dst, enc.encode[:]) -} - -type encoder struct { - src []byte - pos int -} - -func newEncoder(src []byte) *encoder { - return &encoder{ - src: src, - pos: len(src) * 8, - } -} - -func (enc *encoder) next() (byte, bool) { - var i, pos int - var j, blen byte - pos = enc.pos - 6 - if pos <= 0 { - pos = 0 - blen = byte(enc.pos) - } else { - i = pos / 8 - j = byte(pos % 8) - blen = byte((i+1)*8 - pos) - if blen > 6 { - blen = 6 - } - } - shift := 8 - j - blen - b := enc.src[i] >> shift & (1< 0 { - blen1 := 6 - blen - b = b<>(8-blen1) - } - if b&compactMask == compactMask { - if pos > 0 || b > mask5bits { - pos++ - } - b &= mask5bits - } - enc.pos = pos - - return b, pos > 0 -} - -func (enc *encoder) encode(dst []byte, encTable []byte) []byte { - x, hasMore := enc.next() - for { - dst = append(dst, encTable[x]) - if !hasMore { - break - } - x, hasMore = enc.next() - } - return dst -} - -func (enc *encoder) encodeV2(dst []byte, encTable []byte) []byte { - for enc.pos > 0 { - size := 6 - b := enc.get6bits() - if b&compactMask == compactMask { - if enc.pos > 6 || b > mask5bits { - size = 5 - } - b &= mask5bits - } - dst = append(dst, encTable[b]) - enc.pos -= size - } - return dst -} - -func (enc *encoder) get6bits() byte { - r := enc.pos & 0x7 - i := enc.pos >> 3 - if r == 0 { - i, r = i-1, 8 - } - b := enc.src[i] >> (8 - r) - if r < 6 && i > 0 { - b |= enc.src[i-1] << r - } - return b & mask6bits -} - -type CorruptInputError int64 - -func (e CorruptInputError) Error() string { - return "illegal base62 data at input byte " + strconv.FormatInt(int64(e), 10) -} - -// Decode decodes src using the encoding enc, returns the decoded bytes. -// -// If src contains invalid base62 data, it will return nil and CorruptInputError. -func (enc *Encoding) Decode(src []byte) ([]byte, error) { - if len(src) == 0 { - return []byte{}, nil - } - dst := make([]byte, len(src)*6/8+1) - dec := decoder(src) - idx, err := dec.decode(dst, enc.decodeMap[:]) - if err != nil { - return nil, err - } - return dst[idx:], nil -} - -// DecodeString returns the bytes represented by the base62 string src. -func (enc *Encoding) DecodeString(src string) ([]byte, error) { - b := s2b(src) - return enc.Decode(b) -} - -// DecodeToBuf decodes src using the encoding enc, appending the decoded -// bytes to dst. If dst has not enough capacity, it copies dst and returns -// the extended buffer. -// -// If src contains invalid base62 data, it will return nil and CorruptInputError. -func (enc *Encoding) DecodeToBuf(dst []byte, src []byte) ([]byte, error) { - if len(src) == 0 { - return []byte{}, nil - } - oldCap, oldLen := cap(dst), len(dst) - possibleLen := len(src)*6/8 + 1 - if oldCap < oldLen+possibleLen { - newBuf := make([]byte, oldLen, oldLen+possibleLen) - copy(newBuf, dst) - dst = newBuf - } - dec := decoder(src) - idx, err := dec.decode(dst[oldLen:cap(dst)], enc.decodeMap[:]) - if err != nil { - return nil, err - } - if idx != 0 { - copy(dst[oldLen:cap(dst)], dst[oldLen+idx:cap(dst)]) - } - dst = dst[:cap(dst)-idx] - return dst, nil -} - -type decoder []byte - -func (dec decoder) decode(dst []byte, decTable []byte) (int, error) { - idx := len(dst) - pos := byte(0) - b := 0 - for i, c := range dec { - x := decTable[c] - if x == 0xFF { - return 0, CorruptInputError(i) - } - if i == len(dec)-1 { - b |= int(x) << pos - pos += byte(bits.Len8(x)) - } else if x&compactMask == compactMask { - b |= int(x) << pos - pos += 5 - } else { - b |= int(x) << pos - pos += 6 - } - if pos >= 8 { - idx-- - dst[idx] = byte(b) - pos %= 8 - b >>= 8 - } - } - if pos > 0 { - idx-- - dst[idx] = byte(b) - } - return idx, nil -} - -func b2s(b []byte) string { - return *(*string)(unsafe.Pointer(&b)) -} - -func s2b(s string) []byte { - sh := (*reflect.StringHeader)(unsafe.Pointer(&s)) - bh := &reflect.SliceHeader{ - Data: sh.Data, - Len: sh.Len, - Cap: sh.Len, - } - return *(*[]byte)(unsafe.Pointer(bh)) -} diff --git a/vendor/github.com/jxskiss/base62/default.go b/vendor/github.com/jxskiss/base62/default.go deleted file mode 100644 index 71110995..00000000 --- a/vendor/github.com/jxskiss/base62/default.go +++ /dev/null @@ -1,84 +0,0 @@ -package base62 - -const encodeStd = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" - -// StdEncoding is the default base62 encoding using alphabet [A-Za-z0-9]. -var StdEncoding = NewEncoding(encodeStd) - -// Encode encodes src using StdEncoding, returns the encoded bytes. -func Encode(src []byte) []byte { - return StdEncoding.Encode(src) -} - -// EncodeToString returns a base62 string representation of src -// using StdEncoding. -func EncodeToString(src []byte) string { - return StdEncoding.EncodeToString(src) -} - -// EncodeToBuf encodes src using StdEncoding, appending the encoded -// bytes to dst. If dst has not enough capacity, it copies dst and returns -// the extended buffer. -func EncodeToBuf(dst []byte, src []byte) []byte { - return StdEncoding.EncodeToBuf(dst, src) -} - -// Decode decodes src using StdEncoding, returns the decoded bytes. -// -// If src contains invalid base62 data, it will return nil and CorruptInputError. -func Decode(src []byte) ([]byte, error) { - return StdEncoding.Decode(src) -} - -// DecodeString returns the bytes represented by the base62 string src -// using StdEncoding. -func DecodeString(src string) ([]byte, error) { - return StdEncoding.DecodeString(src) -} - -// DecodeToBuf decodes src using StdEncoding, appending the decoded -// bytes to dst. If dst has not enough capacity, it copies dst and returns -// the extended buffer. -// -// If src contains invalid base62 data, it will return nil and CorruptInputError. -func DecodeToBuf(dst []byte, src []byte) ([]byte, error) { - return StdEncoding.DecodeToBuf(dst, src) -} - -// FormatInt encodes an integer num to base62 using StdEncoding. -func FormatInt(num int64) []byte { - return StdEncoding.FormatInt(num) -} - -// FormatUint encodes an unsigned integer num to base62 using StdEncoding. -func FormatUint(num uint64) []byte { - return StdEncoding.FormatUint(num) -} - -// AppendInt appends the base62 representation of the integer num -// using StdEncoding, to dst and returns the extended buffer. -func AppendInt(dst []byte, num int64) []byte { - return StdEncoding.AppendInt(dst, num) -} - -// AppendUint appends the base62 representation of the unsigned integer num -// using StdEncoding, to dst and returns the extended buffer. -func AppendUint(dst []byte, num uint64) []byte { - return StdEncoding.AppendUint(dst, num) -} - -// ParseInt returns an integer from its base62 representation -// using StdEncoding. -// -// If src contains invalid base62 data, it returns 0 and CorruptInputError. -func ParseInt(src []byte) (int64, error) { - return StdEncoding.ParseInt(src) -} - -// ParseUint returns an unsigned integer from its base62 representation -// using StdEncoding. -// -// If src contains invalid base62 data, it returns 0 and CorruptInputError. -func ParseUint(src []byte) (uint64, error) { - return StdEncoding.ParseUint(src) -} diff --git a/vendor/github.com/jxskiss/base62/integer.go b/vendor/github.com/jxskiss/base62/integer.go deleted file mode 100644 index dea22651..00000000 --- a/vendor/github.com/jxskiss/base62/integer.go +++ /dev/null @@ -1,65 +0,0 @@ -package base62 - -// FormatInt encodes an integer num to base62 using the encoding enc. -func (enc *Encoding) FormatInt(num int64) []byte { - dst := make([]byte, 0) - return enc.AppendUint(dst, uint64(num)) -} - -// FormatUint encodes an unsigned integer num to base62 using the encoding enc. -func (enc *Encoding) FormatUint(num uint64) []byte { - dst := make([]byte, 0) - return enc.AppendUint(dst, num) -} - -// AppendInt appends the base62 representation of the integer num, -// as generated by FormatInt, to dst and returns the extended buffer. -func (enc *Encoding) AppendInt(dst []byte, num int64) []byte { - return enc.AppendUint(dst, uint64(num)) -} - -// AppendUint appends the base62 representation of the unsigned integer num, -// as generated by FormatUint, to dst and returns the extended buffer. -func (enc *Encoding) AppendUint(dst []byte, num uint64) []byte { - if num == 0 { - dst = append(dst, enc.encode[0]) - return dst - } - - var buf [11]byte - var i = 11 - for num > 0 { - r := num % base - num /= base - i-- - buf[i] = enc.encode[r] - } - dst = append(dst, buf[i:]...) - return dst -} - -// ParseInt returns an integer from its base62 representation. -// -// If src contains invalid base62 data, it returns 0 and CorruptInputError. -func (enc *Encoding) ParseInt(src []byte) (int64, error) { - num, err := enc.ParseUint(src) - if err != nil { - return 0, err - } - return int64(num), nil -} - -// ParseUint returns an unsigned integer from its base62 representation. -// -// If src contains invalid base62 data, it returns 0 and CorruptInputError. -func (enc *Encoding) ParseUint(src []byte) (uint64, error) { - var num uint64 - for i, c := range src { - x := enc.decodeMap[c] - if x == 0xFF { - return 0, CorruptInputError(i) - } - num = num*base + uint64(x) - } - return num, nil -} diff --git a/vendor/modules.txt b/vendor/modules.txt index 0b441476..59172063 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -350,9 +350,6 @@ github.com/jbenet/go-context/io # github.com/jmespath/go-jmespath v0.4.0 ## explicit; go 1.14 github.com/jmespath/go-jmespath -# github.com/jxskiss/base62 v1.1.0 -## explicit; go 1.13 -github.com/jxskiss/base62 # github.com/kevinburke/ssh_config v1.2.0 ## explicit github.com/kevinburke/ssh_config