bcgit / bc-csharp

BouncyCastle.NET Cryptography Library (Mirror)
https://www.bouncycastle.org/csharp
MIT License
1.68k stars 558 forks source link

sign data with bouncy castle #437

Open bian8021 opened 1 year ago

bian8021 commented 1 year ago

I found following sample code to sign data with SM2 algorithm, why it has a userId parameter here, what the purpose for this parameter, can I pass null value here?

public static byte[] SignSm3WithSm2Asn1Rs(byte[] msg, byte[] userId, AsymmetricKeyParameter privateKey) { try { ISigner signer = SignerUtilities.GetSigner("SM3withSM2"); signer.Init(true, new ParametersWithID(privateKey, userId)); signer.BlockUpdate(msg, 0, msg.Length); byte[] sig = signer.GenerateSignature(); return sig; } catch (Exception e) { log.Error("SignSm3WithSm2Asn1Rs error: " + e.Message, e); return null; } }

peterdettman commented 1 year ago

SM2 uses a user ID as a kind of context parameter. You will have to read about the algorithm for more details.

You can't use null, but you can just pass the privateKey (instead of a ParametersWithID) and the default user ID will be used. Note that anyone trying to verify your signature would need to agree on the user ID involved (whether an explicit one or the default).

bian8021 commented 1 year ago

@peterdettman looks like user ID is a mandatory parameter. could you provide code snippet how to pass private key without user ID?

bian8021 commented 1 year ago

@peterdettman any clue here?