8000 jwe/CompactSerialize: improve performance. (#67) · go-jose/go-jose@aa386df · GitHub
[go: up one dir, main page]

Skip to content

Commit aa386df

Browse files
authored
jwe/CompactSerialize: improve performance. (#67)
Avoid fmt.Sprintf, which is unnecessarily slow. Instead allocate a `[]byte` and write directly to that.
1 parent 053c9bf commit aa386df

File tree

3 files changed

+48
-12
lines changed

3 files changed

+48
-12
lines changed

encoding.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,36 @@ func base64URLDecode(value string) ([]byte, error) {
189189
value = strings.TrimRight(value, "=")
190190
return base64.RawURLEncoding.DecodeString(value)
191191
}
192+
193+
func base64EncodeLen(sl []byte) int {
194+
return base64.RawURLEncoding.EncodedLen(len(sl))
195+
}
196+
197+
func base64JoinWithDots(inputs ...[]byte) string {
198+
if len(inputs) == 0 {
199+
return ""
200+
}
201+
202+
// Count of dots.
203+
totalCount := len(inputs) - 1
204+
205+
for _, input := range inputs {
206+
totalCount += base64EncodeLen(input)
207+
}
208+
209+
out := make([]byte, totalCount)
210+
startEncode := 0
211+
for i, input := range inputs {
212+
base64.RawURLEncoding.Encode(out[startEncode:], input)
213+
214+
if i == len(inputs)-1 {
215+
continue
216+
}
217+
218+
startEncode += base64EncodeLen(input)
219+
out[startEncode] = '.'
220+
startEncode++
221+
}
222+
223+
return string(out)
224+
}

jwe.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -252,13 +252,13 @@ func (obj JSONWebEncryption) CompactSerialize() (string, error) {
252252

253253
serializedProtected := mustSerializeJSON(obj.protected)
254254

255-
return fmt.Sprintf(
256-
"%s.%s.%s.%s.%s",
257-
base64.RawURLEncoding.EncodeToString(serializedProtected),
258-
base64.RawURLEncoding.EncodeToString(obj.recipients[0].encryptedKey),
259-
base64.RawURLEncoding.EncodeToString(obj.iv),
260-
base64.RawURLEncoding.EncodeToString(obj.ciphertext),
261-
base64.RawURLEncoding.EncodeToString(obj.tag)), nil
255+
return base64JoinWithDots(
256+
serializedProtected,
257+
obj.recipients[0].encryptedKey,
258+
obj.iv,
259+
obj.ciphertext,
260+
obj.tag,
261+
), nil
262262
}
263263

264264
// FullSerialize serializes an object using the full JSON serialization format.

jws.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -314,15 +314,18 @@ func (obj JSONWebSignature) compactSerialize(detached bool) (string, error) {
314314
return "", ErrNotSupported
315315
}
316316

317-
serializedProtected := base64.RawURLEncoding.EncodeToString(mustSerializeJSON(obj.Signatures[0].protected))
318-
payload := ""
319-
signature := base64.RawURLEncoding.EncodeToString(obj.Signatures[0].Signature)
317+
serializedProtected := mustSerializeJSON(obj.Signatures[0].protected)
320318

319+
var payload []byte
321320
if !detached {
322-
payload = base64.RawURLEncoding.EncodeToString(obj.payload)
321+
payload = obj.payload
323322
}
324323

325-
return fmt.Sprintf("%s.%s.%s", serializedProtected, payload, signature), nil
324+
return base64JoinWithDots(
325+
serializedProtected,
326+
payload,
327+
obj.Signatures[0].Signature,
328+
), nil
326329
}
327330

328331
// CompactSerialize serializes an object using the compact serialization format.

0 commit comments

Comments
 (0)
0