PlainDsaEncoding
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Utilities;
using System;
namespace Org.BouncyCastle.Crypto.Signers
{
public class PlainDsaEncoding : IDsaEncoding
{
public static readonly PlainDsaEncoding Instance = new PlainDsaEncoding();
public virtual BigInteger[] Decode(BigInteger n, byte[] encoding)
{
int unsignedByteLength = BigIntegers.GetUnsignedByteLength(n);
if (encoding.Length != unsignedByteLength * 2)
throw new ArgumentException("Encoding has incorrect length", "encoding");
return new BigInteger[2] {
DecodeValue(n, encoding, 0, unsignedByteLength),
DecodeValue(n, encoding, unsignedByteLength, unsignedByteLength)
};
}
public virtual byte[] Encode(BigInteger n, BigInteger r, BigInteger s)
{
int unsignedByteLength = BigIntegers.GetUnsignedByteLength(n);
byte[] array = new byte[unsignedByteLength * 2];
EncodeValue(n, r, array, 0, unsignedByteLength);
EncodeValue(n, s, array, unsignedByteLength, unsignedByteLength);
return array;
}
public virtual int Encode(BigInteger n, BigInteger r, BigInteger s, Span<byte> output)
{
int unsignedByteLength = BigIntegers.GetUnsignedByteLength(n);
int num = unsignedByteLength * 2;
EncodeValue(n, r, output.Slice(0, unsignedByteLength));
int num2 = unsignedByteLength;
EncodeValue(n, s, output.Slice(num2, num - num2));
return num;
}
public virtual int GetMaxEncodingSize(BigInteger n)
{
return BigIntegers.GetUnsignedByteLength(n) * 2;
}
protected virtual BigInteger CheckValue(BigInteger n, BigInteger x)
{
if (x.SignValue < 0 || x.CompareTo(n) >= 0)
throw new ArgumentException("Value out of range", "x");
return x;
}
protected virtual BigInteger DecodeValue(BigInteger n, byte[] buf, int off, int len)
{
return CheckValue(n, new BigInteger(1, buf, off, len));
}
protected virtual void EncodeValue(BigInteger n, BigInteger x, byte[] buf, int off, int len)
{
byte[] array = CheckValue(n, x).ToByteArrayUnsigned();
int num = System.Math.Max(0, array.Length - len);
int num2 = array.Length - num;
int num3 = len - num2;
Arrays.Fill(buf, off, off + num3, 0);
Array.Copy(array, num, buf, off + num3, num2);
}
protected virtual void EncodeValue(BigInteger n, BigInteger x, Span<byte> buffer)
{
byte[] array = CheckValue(n, x).ToByteArrayUnsigned();
int num = System.Math.Max(0, array.Length - buffer.Length);
int num2 = array.Length - num;
int num3 = buffer.Length - num2;
Span<byte> span = buffer.Slice(0, num3);
span.Fill(0);
span = array.AsSpan(num, num2);
int num4 = num3;
span.CopyTo(buffer.Slice(num4, buffer.Length - num4));
}
}
}