-
Notifications
You must be signed in to change notification settings - Fork 414
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reduce allocation with Base64UrlEncoder #2162
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,6 @@ namespace Microsoft.IdentityModel.JsonWebTokens | |
/// </summary> | ||
public class JsonWebToken : SecurityToken | ||
{ | ||
private char[] _hChars; | ||
private ClaimsIdentity _claimsIdentity; | ||
private bool _wasClaimsIdentitySet; | ||
|
||
|
@@ -470,25 +469,39 @@ private void ReadToken(string encodedJson) | |
throw LogHelper.LogExceptionMessage(new SecurityTokenMalformedException(LogHelper.FormatInvariant(LogMessages.IDX14310, encodedJson))); | ||
|
||
// right number of dots for JWE | ||
_hChars = encodedJson.ToCharArray(0, Dot1); | ||
ReadOnlyMemory<char> hChars = encodedJson.AsMemory(0, Dot1); | ||
stephentoub marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// header cannot be empty | ||
if (_hChars.Length == 0) | ||
if (hChars.IsEmpty) | ||
throw LogHelper.LogExceptionMessage(new ArgumentException(LogHelper.FormatInvariant(LogMessages.IDX14307, encodedJson))); | ||
|
||
HeaderAsciiBytes = Encoding.ASCII.GetBytes(_hChars); | ||
byte[] headerAsciiBytes = new byte[hChars.Length]; | ||
#if NET6_0_OR_GREATER | ||
Encoding.ASCII.GetBytes(hChars.Span, headerAsciiBytes); | ||
#else | ||
unsafe | ||
{ | ||
fixed (char* hCharsPtr = hChars.Span) | ||
fixed (byte* headerAsciiBytesPtr = headerAsciiBytes) | ||
{ | ||
Encoding.ASCII.GetBytes(hCharsPtr, hChars.Length, headerAsciiBytesPtr, headerAsciiBytes.Length); | ||
} | ||
} | ||
#endif | ||
HeaderAsciiBytes = headerAsciiBytes; | ||
|
||
try | ||
{ | ||
Header = new JsonClaimSet(Base64UrlEncoder.UnsafeDecode(_hChars)); | ||
Header = new JsonClaimSet(Base64UrlEncoder.UnsafeDecode(hChars)); | ||
} | ||
catch (Exception ex) | ||
{ | ||
throw LogHelper.LogExceptionMessage(new ArgumentException(LogHelper.FormatInvariant(LogMessages.IDX14102, encodedJson.Substring(0, Dot1), encodedJson), ex)); | ||
} | ||
|
||
// dir does not have any key bytes | ||
char[] encryptedKeyBytes = encodedJson.ToCharArray(Dot1 + 1, Dot2 - Dot1 - 1); | ||
if (encryptedKeyBytes.Length != 0) | ||
ReadOnlyMemory<char> encryptedKeyBytes = encodedJson.AsMemory(Dot1 + 1, Dot2 - Dot1 - 1); | ||
if (!encryptedKeyBytes.IsEmpty) | ||
{ | ||
EncryptedKeyBytes = Base64UrlEncoder.UnsafeDecode(encryptedKeyBytes); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a comment for existing code, but all the other calls to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
_encryptedKey = encodedJson.Substring(Dot1 + 1, Dot2 - Dot1 - 1); | ||
|
@@ -498,8 +511,8 @@ private void ReadToken(string encodedJson) | |
_encryptedKey = string.Empty; | ||
} | ||
|
||
char[] initializationVectorChars = encodedJson.ToCharArray(Dot2 + 1, Dot3 - Dot2 - 1); | ||
if (initializationVectorChars.Length == 0) | ||
ReadOnlyMemory<char> initializationVectorChars = encodedJson.AsMemory(Dot2 + 1, Dot3 - Dot2 - 1); | ||
if (initializationVectorChars.IsEmpty) | ||
throw LogHelper.LogExceptionMessage(new ArgumentException(LogHelper.FormatInvariant(LogMessages.IDX14308, encodedJson))); | ||
|
||
try | ||
|
@@ -511,8 +524,8 @@ private void ReadToken(string encodedJson) | |
throw LogHelper.LogExceptionMessage(new ArgumentException(LogHelper.FormatInvariant(LogMessages.IDX14309, encodedJson, encodedJson), ex)); | ||
} | ||
|
||
char[] authTagChars = encodedJson.ToCharArray(Dot4 + 1, encodedJson.Length - Dot4 - 1); | ||
if (authTagChars.Length == 0) | ||
ReadOnlyMemory<char> authTagChars = encodedJson.AsMemory(Dot4 + 1); | ||
if (authTagChars.IsEmpty) | ||
throw LogHelper.LogExceptionMessage(new ArgumentException(LogHelper.FormatInvariant(LogMessages.IDX14310, encodedJson))); | ||
|
||
try | ||
|
@@ -524,13 +537,13 @@ private void ReadToken(string encodedJson) | |
throw LogHelper.LogExceptionMessage(new ArgumentException(LogHelper.FormatInvariant(LogMessages.IDX14311, encodedJson, encodedJson), ex)); | ||
} | ||
|
||
char[] cipherTextBytes = encodedJson.ToCharArray(Dot3 + 1, Dot4 - Dot3 - 1); | ||
if (cipherTextBytes.Length == 0) | ||
ReadOnlyMemory<char> cipherTextBytes = encodedJson.AsMemory(Dot3 + 1, Dot4 - Dot3 - 1); | ||
if (cipherTextBytes.IsEmpty) | ||
throw LogHelper.LogExceptionMessage(new ArgumentException(LogHelper.FormatInvariant(LogMessages.IDX14306, encodedJson))); | ||
|
||
try | ||
{ | ||
CipherTextBytes = Base64UrlEncoder.UnsafeDecode(encodedJson.ToCharArray(Dot3 + 1, Dot4 - Dot3 - 1)); | ||
CipherTextBytes = Base64UrlEncoder.UnsafeDecode(cipherTextBytes); | ||
} | ||
catch (Exception ex) | ||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(nit) what does
h
mean inhChars
? "header"?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No idea, I just kept the original naming :)