/// <summary> /// Attempts to create the ECDSA signature for the specified hash value in the indicated format /// into the provided buffer. /// </summary> /// <param name="hash">The hash value to sign.</param> /// <param name="destination">The buffer to receive the signature.</param> /// <param name="signatureFormat">The encoding format to use for the signature.</param> /// <param name="bytesWritten"> /// When this method returns, contains a value that indicates the number of bytes written to /// <paramref name="destination"/>. This parameter is treated as uninitialized. /// </param> /// <returns> /// <see langword="true"/> if <paramref name="destination"/> is big enough to receive the signature; /// otherwise, <see langword="false"/>. /// </returns> /// <exception cref="CryptographicException"> /// An error occurred in the signing operation. /// </exception> protected virtual bool TrySignHashCore( ReadOnlySpan <byte> hash, Span <byte> destination, DSASignatureFormat signatureFormat, out int bytesWritten) { // This method is expected to be overridden with better implementation // The only available implementation here is abstract method, use it byte[] result = SignHash(hash.ToArray()); byte[] converted = AsymmetricAlgorithmHelpers.ConvertFromIeeeP1363Signature(result, signatureFormat); return(Helpers.TryCopyToDestination(converted, destination, out bytesWritten)); }
/// <summary> /// Creates the DSA signature for the specified hash value in the indicated format. /// </summary> /// <param name="hash">The hash value to sign.</param> /// <param name="signatureFormat">The encoding format to use for the signature.</param> /// <returns> /// The DSA signature for the specified data. /// </returns> /// <exception cref="CryptographicException"> /// An error occurred in the signing operation. /// </exception> protected virtual byte[] CreateSignatureCore(ReadOnlySpan <byte> hash, DSASignatureFormat signatureFormat) { Span <byte> signature = stackalloc byte[SignatureStackSize]; if (TryCreateSignatureCore(hash, signature, signatureFormat, out int bytesWritten)) { return(signature.Slice(0, bytesWritten).ToArray()); } // If that didn't work, fall back to the older overload and convert formats. byte[] sig = CreateSignature(hash.ToArray()); return(AsymmetricAlgorithmHelpers.ConvertFromIeeeP1363Signature(sig, signatureFormat)); }
/// <summary> /// Attempts to create the DSA signature for the specified hash value in the indicated format /// into the provided buffer. /// </summary> /// <param name="hash">The hash value to sign.</param> /// <param name="destination">The buffer to receive the signature.</param> /// <param name="signatureFormat">The encoding format to use for the signature.</param> /// <param name="bytesWritten"> /// When this method returns, contains a value that indicates the number of bytes written to /// <paramref name="destination"/>. This parameter is treated as uninitialized. /// </param> /// <returns> /// <see langword="true"/> if <paramref name="destination"/> is big enough to receive the signature; /// otherwise, <see langword="false"/>. /// </returns> /// <exception cref="CryptographicException"> /// An error occurred in the signing operation. /// </exception> protected virtual bool TryCreateSignatureCore( ReadOnlySpan <byte> hash, Span <byte> destination, DSASignatureFormat signatureFormat, out int bytesWritten) { // This method is expected to be overriden with better implementation // The only available implementation here is abstract method, use it byte[] sig = CreateSignature(hash.ToArray()); if (signatureFormat != DSASignatureFormat.IeeeP1363FixedFieldConcatenation) { sig = AsymmetricAlgorithmHelpers.ConvertFromIeeeP1363Signature(sig, signatureFormat); } return(Helpers.TryCopyToDestination(sig, destination, out bytesWritten)); }
/// <summary> /// Computes the hash value of the specified data and signs it using the specified signature format. /// </summary> /// <param name="data">The data to sign.</param> /// <param name="hashAlgorithm">The hash algorithm to use to create the hash value.</param> /// <param name="signatureFormat">The encoding format to use for the signature.</param> /// <returns> /// The DSA signature for the specified data. /// </returns> /// <exception cref="CryptographicException"> /// An error occurred in the hashing or signing operation. /// </exception> protected virtual byte[] SignDataCore( ReadOnlySpan <byte> data, HashAlgorithmName hashAlgorithm, DSASignatureFormat signatureFormat) { Span <byte> signature = stackalloc byte[SignatureStackSize]; if (TrySignDataCore(data, signature, hashAlgorithm, signatureFormat, out int bytesWritten)) { return(signature.Slice(0, bytesWritten).ToArray()); } // If that didn't work, fall back on older approaches. byte[] hash = HashSpanToArray(data, hashAlgorithm); byte[] sig = CreateSignature(hash); return(AsymmetricAlgorithmHelpers.ConvertFromIeeeP1363Signature(sig, signatureFormat)); }