You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.9 KiB
60 lines
1.9 KiB
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR) |
|
#pragma warning disable |
|
using System; |
|
|
|
using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1; |
|
using BestHTTP.SecureProtocol.Org.BouncyCastle.Math; |
|
using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities; |
|
|
|
namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Signers |
|
{ |
|
public class StandardDsaEncoding |
|
: IDsaEncoding |
|
{ |
|
public static readonly StandardDsaEncoding Instance = new StandardDsaEncoding(); |
|
|
|
public virtual BigInteger[] Decode(BigInteger n, byte[] encoding) |
|
{ |
|
Asn1Sequence seq = (Asn1Sequence)Asn1Object.FromByteArray(encoding); |
|
if (seq.Count == 2) |
|
{ |
|
BigInteger r = DecodeValue(n, seq, 0); |
|
BigInteger s = DecodeValue(n, seq, 1); |
|
|
|
byte[] expectedEncoding = Encode(n, r, s); |
|
if (Arrays.AreEqual(expectedEncoding, encoding)) |
|
return new BigInteger[]{ r, s }; |
|
} |
|
|
|
throw new ArgumentException("Malformed signature", "encoding"); |
|
} |
|
|
|
public virtual byte[] Encode(BigInteger n, BigInteger r, BigInteger s) |
|
{ |
|
return new DerSequence( |
|
EncodeValue(n, r), |
|
EncodeValue(n, s) |
|
).GetEncoded(Asn1Encodable.Der); |
|
} |
|
|
|
protected virtual BigInteger CheckValue(BigInteger n, BigInteger x) |
|
{ |
|
if (x.SignValue < 0 || (null != n && x.CompareTo(n) >= 0)) |
|
throw new ArgumentException("Value out of range", "x"); |
|
|
|
return x; |
|
} |
|
|
|
protected virtual BigInteger DecodeValue(BigInteger n, Asn1Sequence s, int pos) |
|
{ |
|
return CheckValue(n, ((DerInteger)s[pos]).Value); |
|
} |
|
|
|
protected virtual DerInteger EncodeValue(BigInteger n, BigInteger x) |
|
{ |
|
return new DerInteger(CheckValue(n, x)); |
|
} |
|
} |
|
} |
|
#pragma warning restore |
|
#endif
|
|
|