|
1 | 1 | using System;
|
2 | 2 | using System.Buffers.Binary;
|
3 | 3 | using System.Collections.Generic;
|
| 4 | +using System.Formats.Asn1; |
4 | 5 | using System.IO;
|
5 | 6 | using System.Linq;
|
6 | 7 | using System.Security.Cryptography;
|
@@ -718,16 +719,23 @@ public void TestAuthenticatorData()
|
718 | 719 | Assert.True(ad.Extensions.GetBytes().SequenceEqual(extBytes));
|
719 | 720 | }
|
720 | 721 |
|
721 |
| - internal static byte[] EcDsaSigFromSig(byte[] sig, int keySize) |
| 722 | + internal static byte[] EcDsaSigFromSig(ReadOnlySpan<byte> sig, int keySize) |
722 | 723 | {
|
723 | 724 | var coefficientSize = (int)Math.Ceiling((decimal)keySize / 8);
|
724 |
| - var R = sig.Take(coefficientSize); |
725 |
| - var S = sig.TakeLast(coefficientSize); |
| 725 | + var r = sig.Slice(0, coefficientSize); |
| 726 | + var s = sig.Slice(sig.Length - coefficientSize); |
726 | 727 |
|
727 |
| - var intR = AsnElt.MakeInteger(R.ToArray()); |
728 |
| - var intS = AsnElt.MakeInteger(S.ToArray()); |
729 |
| - var ecdsasig = AsnElt.Make(AsnElt.SEQUENCE, new AsnElt[] { intR, intS }); |
730 |
| - return ecdsasig.Encode(); |
| 728 | + var writer = new AsnWriter(AsnEncodingRules.BER); |
| 729 | + |
| 730 | + ReadOnlySpan<byte> zero = new byte[1] { 0 }; |
| 731 | + |
| 732 | + using (writer.PushSequence()) |
| 733 | + { |
| 734 | + writer.WriteIntegerUnsigned(r.TrimStart(zero)); |
| 735 | + writer.WriteIntegerUnsigned(s.TrimStart(zero)); |
| 736 | + } |
| 737 | + |
| 738 | + return writer.Encode(); |
731 | 739 | }
|
732 | 740 |
|
733 | 741 | [Fact]
|
|
0 commit comments