I don't find any reference for deterministic with P1363 format. the r
and s
always different in deterministic mode.
Non-Deterministic with P1363 -> WithPlain-ECDSA
OK
ISigner sign = SignerUtilities.GetSigner("SHA256WithPlain-ECDSA");
sign.Init(true, privateKey);
sign.BlockUpdate(message, 0, message.Length);
byte[] signedBytes = sign.GenerateSignature(); // Signature can divded by 2. OK
Deterministic way: (How to apply WithPlain-ECDSA
?)
var signer = new ECDsaSigner(new HMacDsaKCalculator(new Sha256Digest()));
signer.Init(true, privateKey);
var toto = signer.GenerateSignature(message);
var r = toto[0].ToByteArray(); // sometimes r are equqal s
var s = toto[1].ToByteArray(); // sometimes s larger than s byte one.
I can't return r.Concat(s).ToArray()
. There's no way to convert r
and s
to byte[]
untill it converted to P1363? which doesn't exist in deterministic version above...
- How to concatenate
r
ands
in byte[] in P1363 format? - How to make sure I can convert it back through take half of array for
r
and other half fors
. - Sometimes
r
equals
, ors
larger thanr
, How to make sure exactly both are equal when generated?
I don't find any reference for P1363. But I found only ASN1 DER format:
using (MemoryStream ms = new MemoryStream())
using (Asn1OutputStream asn1stream = new Asn1OutputStream(ms))
{
DerSequenceGenerator seq = new DerSequenceGenerator(asn1stream);
seq.AddObject(new DerInteger(s));
seq.AddObject(new DerInteger(s));
seq.Close();
var arrr = ms.ToArray();
}
Am also tried this trick
But I looped 1000 and check if arrays of r.Length
== s.Length
, No equality in all loop.. As he use BigInteger.ToByteArrayUnsigned()
.
Not sure exactly 100% if above guy's trick work, But the guy try to generate it in fixed way:
Array.Copy(sig1, 0, sig, 0 + (32 - sig1.Length), sig1.Length);
Array.Copy(sig2, 0, sig, 32 + (32 - sig2.Length), sig2.Length);
Short Answer:
According to @Topaco answer, its fast manual approach. Many thanks to him. There's also two classes we can use after investigation StandardDsaEncoding
and PlainDsaEncoding
classes
StandardDsaEncoding
used to encode/decode ASN1.Der format.PlainDsaEncoding
used to encode/decode P1363 format.
PlainDsaEncoding.Instance.Encode()
// This convert to P1363 format.
PlainDsaEncoding.Instance.Decode()
// Decode to signature BigInteger[]
again. (r, s)
Note: ECDsaSigner
can wrapped inside DsaDigestSigner
to allow hash of message automatically. or compute hash(message) manually then use ECDsaSigner
exactly like @Topaco other post answer.