public static bool TryWrite(Span <byte> output, Sha256 hash, string keyType, string verb, string resourceId, string resourceType, string tokenVersion, DateTime utc, out int bytesWritten) { int written, consumed, totalWritten = 0; bytesWritten = 0; Span <byte> buffer; unsafe { var pBuffer = stackalloc byte[AuthenticationHeaderBufferSize]; buffer = new Span <byte>(pBuffer, AuthenticationHeaderBufferSize); } s_type.CopyTo(buffer); totalWritten += s_type.Length; var bufferSlice = buffer.Slice(totalWritten); if (Utf16.ToUtf8(keyType.AsReadOnlySpan().AsBytes(), bufferSlice, out consumed, out written) != TransformationStatus.Done) { throw new NotImplementedException("need to resize buffer"); } totalWritten += written; bufferSlice = buffer.Slice(totalWritten); s_ver.CopyTo(bufferSlice); totalWritten += s_ver.Length; bufferSlice = buffer.Slice(totalWritten); if (Utf16.ToUtf8(tokenVersion.AsReadOnlySpan().AsBytes(), bufferSlice, out consumed, out written) != TransformationStatus.Done) { throw new NotImplementedException("need to resize buffer"); } totalWritten += written; bufferSlice = buffer.Slice(totalWritten); s_sig.CopyTo(bufferSlice); totalWritten += s_sig.Length; var front = buffer.Slice(0, totalWritten); var payload = buffer.Slice(totalWritten); totalWritten = 0; if (verb.Equals("GET", StringComparison.Ordinal) || verb.Equals("get", StringComparison.Ordinal)) { s_get.CopyTo(payload); totalWritten += s_get.Length; } else if (verb.Equals("POST", StringComparison.Ordinal) || verb.Equals("post", StringComparison.Ordinal)) { s_post.CopyTo(payload); totalWritten += s_post.Length; } else if (verb.Equals("DELETE", StringComparison.Ordinal) || verb.Equals("delete", StringComparison.Ordinal)) { s_delete.CopyTo(payload); totalWritten += s_delete.Length; } else { if (Utf16.ToUtf8(verb.AsReadOnlySpan().AsBytes(), payload, out consumed, out written) != TransformationStatus.Done) { throw new NotImplementedException("need to resize buffer"); } if (Ascii.ToLowerInPlace(payload.Slice(0, written), out written) != TransformationStatus.Done) { throw new NotImplementedException("need to resize buffer"); } payload[written] = (byte)'\n'; totalWritten += written + 1; } bufferSlice = payload.Slice(totalWritten); if (Utf16.ToUtf8(resourceType.AsReadOnlySpan().AsBytes(), bufferSlice, out consumed, out written) != TransformationStatus.Done) { throw new NotImplementedException("need to resize buffer"); } if (Ascii.ToLowerInPlace(bufferSlice.Slice(0, written), out written) != TransformationStatus.Done) { throw new NotImplementedException("need to resize buffer"); } bufferSlice[written] = (byte)'\n'; totalWritten += written + 1; bufferSlice = payload.Slice(totalWritten); if (Utf16.ToUtf8(resourceId.AsReadOnlySpan().AsBytes(), bufferSlice, out consumed, out written) != TransformationStatus.Done) { throw new NotImplementedException("need to resize buffer"); } bufferSlice[written] = (byte)'\n'; totalWritten += written + 1; bufferSlice = payload.Slice(totalWritten); if (!Text.Formatters.Utf8.TryFormat(utc, bufferSlice, out written, 'l')) { throw new NotImplementedException("need to resize buffer"); } bufferSlice[written] = (byte)'\n'; totalWritten += written + 1; bufferSlice = payload.Slice(totalWritten); bufferSlice[0] = (byte)'\n'; totalWritten += 1; hash.Append(buffer.Slice(front.Length, totalWritten)); hash.GetHash(buffer.Slice(front.Length, hash.OutputSize)); if (!Base64.EncodeInPlace(buffer.Slice(front.Length), hash.OutputSize, out written)) { throw new NotImplementedException("need to resize buffer"); } var len = front.Length + written; if (!UrlEncoder.TryEncode(buffer.Slice(0, len), output, out bytesWritten)) { bytesWritten = 0; return(false); } return(true); }