diff --git a/src/Compat.cs b/src/Compat.cs index 6ac6344..5835e06 100644 --- a/src/Compat.cs +++ b/src/Compat.cs @@ -1,4 +1,5 @@ using System; +using System.Buffers.Binary; using System.Collections.Generic; using System.ComponentModel; using System.Linq; @@ -61,14 +62,13 @@ namespace WTelegram return new IPEndPoint(IPAddress.Parse(addr[0..colon]), int.Parse(addr[(colon + 1)..])); } - private static readonly byte[] PemStart = new byte[] { 0x30, 0x82, 0x01, 0x0A, 0x02, 0x82, 0x01, 0x01 }; internal static void ImportFromPem(this RSA rsa, string pem) { var header = pem.IndexOf("-----BEGIN RSA PUBLIC KEY-----"); var footer = pem.IndexOf("-----END RSA PUBLIC KEY-----"); if (header == -1 || footer <= header) throw new ArgumentException("Invalid RSA Public Key"); byte[] bytes = System.Convert.FromBase64String(pem[(header+30)..footer]); - if (bytes.Length != 270 || !bytes.Take(8).SequenceEqual(PemStart) || bytes[265] != 0x02 || bytes[266] != 0x03) + if (bytes.Length != 270 || BinaryPrimitives.ReadInt64BigEndian(bytes) != 0x3082010A02820101 || bytes[265] != 0x02 || bytes[266] != 0x03) throw new ArgumentException("Unrecognized sequence in RSA Public Key"); rsa.ImportParameters(new RSAParameters { Modulus = bytes[8..265], Exponent = bytes[267..270] }); }