Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#736 Generate hash and sign/verify data for secrettext values #1005

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,17 @@ codeunit 1266 "Cryptography Management"
begin
end;

/// <summary>
/// Generates a hash from a string based on the provided hash algorithm.
/// </summary>
/// <param name="InputString">Input string.</param>
/// <param name="HashAlgorithmType">The available hash algorithms include MD5, SHA1, SHA256, SHA384, and SHA512.</param>
/// <returns>Hashed value.</returns>
procedure GenerateHash(InputString: SecretText; HashAlgorithmType: Option MD5,SHA1,SHA256,SHA384,SHA512): Text
begin
exit(CryptographyManagementImpl.GenerateHash(InputString, HashAlgorithmType));
end;

/// <summary>
/// Generates a hash from a string based on the provided hash algorithm.
/// </summary>
Expand Down Expand Up @@ -132,6 +143,18 @@ codeunit 1266 "Cryptography Management"
end;
#endif

/// <summary>
/// Generates a keyed hash from a string based on provided hash algorithm and key.
/// </summary>
/// <param name="InputString">Input string.</param>
/// <param name="Key">Key to use in the hash algorithm.</param>
/// <param name="HashAlgorithmType">The available hash algorithms include HMACMD5, HMACSHA1, HMACSHA256, HMACSHA384, and HMACSHA512.</param>
/// <returns>Hashed value.</returns>
procedure GenerateHash(InputString: SecretText; "Key": SecretText; HashAlgorithmType: Option HMACMD5,HMACSHA1,HMACSHA256,HMACSHA384,HMACSHA512): Text
begin
exit(CryptographyManagementImpl.GenerateHash(InputString, Key, HashAlgorithmType));
end;

/// <summary>
/// Generates a keyed hash from a string based on provided hash algorithm and key.
/// </summary>
Expand All @@ -155,6 +178,17 @@ codeunit 1266 "Cryptography Management"
exit(CryptographyManagementImpl.GenerateHash(InputInStream, HashAlgorithmType));
end;

/// <summary>
/// Generates a base64 encoded hash from a string based on provided hash algorithm.
/// </summary>
/// <param name="InputString">Input string.</param>
/// <param name="HashAlgorithmType">The available hash algorithms include MD5, SHA1, SHA256, SHA384, and SHA512.</param>
/// <returns>Base64 hashed value.</returns>
procedure GenerateHashAsBase64String(InputString: SecretText; HashAlgorithmType: Option MD5,SHA1,SHA256,SHA384,SHA512): Text
begin
exit(CryptographyManagementImpl.GenerateHashAsBase64String(InputString, HashAlgorithmType));
end;

/// <summary>
/// Generates a base64 encoded hash from a string based on provided hash algorithm.
/// </summary>
Expand Down Expand Up @@ -182,6 +216,18 @@ codeunit 1266 "Cryptography Management"
end;
#endif

/// <summary>
/// Generates a keyed base64 encoded hash from a string based on provided hash algorithm and key.
/// </summary>
/// <param name="InputString">Input string.</param>
/// <param name="Key">Key to use in the hash algorithm.</param>
/// <param name="HashAlgorithmType">The available hash algorithms include HMACMD5, HMACSHA1, HMACSHA256, HMACSHA384, and HMACSHA512.</param>
/// <returns>Base64 hashed value.</returns>
procedure GenerateHashAsBase64String(InputString: SecretText; "Key": SecretText; HashAlgorithmType: Option HMACMD5,HMACSHA1,HMACSHA256,HMACSHA384,HMACSHA512): Text
begin
exit(CryptographyManagementImpl.GenerateHashAsBase64String(InputString, Key, HashAlgorithmType));
end;

/// <summary>
/// Generates a keyed base64 encoded hash from a string based on provided hash algorithm and key.
/// </summary>
Expand Down Expand Up @@ -210,6 +256,18 @@ codeunit 1266 "Cryptography Management"
end;
#endif

/// <summary>
/// Generates keyed base64 encoded hash from provided string based on provided hash algorithm and base64 key.
/// </summary>
/// <param name="InputString">Input string.</param>
/// <param name="Key">Key to use in the hash algorithm.</param>
/// <param name="HashAlgorithmType">The available hash algorithms include HMACMD5, HMACSHA1, HMACSHA256, HMACSHA384, and HMACSHA512.</param>
/// <returns>Base64 hashed value.</returns>
procedure GenerateBase64KeyedHashAsBase64String(InputString: SecretText; "Key": SecretText; HashAlgorithmType: Option HMACMD5,HMACSHA1,HMACSHA256,HMACSHA384,HMACSHA512): Text
begin
exit(CryptographyManagementImpl.GenerateBase64KeyedHashAsBase64String(InputString, Key, HashAlgorithmType));
end;

/// <summary>
/// Generates keyed base64 encoded hash from provided string based on provided hash algorithm and base64 key.
/// </summary>
Expand Down Expand Up @@ -238,6 +296,18 @@ codeunit 1266 "Cryptography Management"
end;
#endif

/// <summary>
/// Generates keyed base64 encoded hash from provided string based on provided hash algorithm and base64 key.
/// </summary>
/// <param name="InputString">Input string.</param>
/// <param name="Key">Key to use in the hash algorithm.</param>
/// <param name="HashAlgorithmType">The available hash algorithms include HMACMD5, HMACSHA1, HMACSHA256, HMACSHA384, and HMACSHA512.</param>
/// <returns>Hashed value.</returns>
procedure GenerateBase64KeyedHash(InputString: SecretText; "Key": SecretText; HashAlgorithmType: Option HMACMD5,HMACSHA1,HMACSHA256,HMACSHA384,HMACSHA512): Text
begin
exit(CryptographyManagementImpl.GenerateBase64KeyedHash(InputString, Key, HashAlgorithmType));
end;

/// <summary>
/// Generates keyed base64 encoded hash from provided string based on provided hash algorithm and base64 key.
/// </summary>
Expand Down Expand Up @@ -278,6 +348,18 @@ codeunit 1266 "Cryptography Management"
CryptographyManagementImpl.SignData(InputString, XmlString, HashAlgorithm, SignatureOutStream);
end;

/// <summary>
/// Computes the hash value of the specified string and signs it.
/// </summary>
/// <param name="InputString">Input string for signing.</param>
/// <param name="XmlString">The private key to use in the hash algorithm.</param>
/// <param name="HashAlgorithm">The available hash algorithms are MD5, SHA1, SHA256, SHA384, and SHA512.</param>
/// <param name="SignatureOutStream">The stream to write the signature for the specified string.</param>
procedure SignData(InputString: SecretText; XmlString: SecretText; HashAlgorithm: Enum "Hash Algorithm"; SignatureOutStream: OutStream)
begin
CryptographyManagementImpl.SignData(InputString, XmlString, HashAlgorithm, SignatureOutStream);
end;

#if not CLEAN24
/// <summary>
/// Computes the hash value of the specified data and signs it.
Expand Down Expand Up @@ -306,6 +388,18 @@ codeunit 1266 "Cryptography Management"
CryptographyManagementImpl.SignData(DataInStream, XmlString, HashAlgorithm, SignatureOutStream);
end;

/// <summary>
/// Computes the hash value of the specified string and signs it.
/// </summary>
/// <param name="InputString">Input string for signing.</param>
/// <param name="SignatureKey">The private key to use in the hash algorithm.</param>
/// <param name="HashAlgorithm">The available hash algorithms are MD5, SHA1, SHA256, SHA384, and SHA512.</param>
/// <param name="SignatureOutStream">The stream to write the signature for the specified string.</param>
procedure SignData(InputString: SecretText; SignatureKey: Codeunit "Signature Key"; HashAlgorithm: Enum "Hash Algorithm"; SignatureOutStream: OutStream)
begin
CryptographyManagementImpl.SignData(InputString, SignatureKey, HashAlgorithm, SignatureOutStream);
end;

/// <summary>
/// Computes the hash value of the specified string and signs it.
/// </summary>
Expand All @@ -330,6 +424,19 @@ codeunit 1266 "Cryptography Management"
CryptographyManagementImpl.SignData(DataInStream, SignatureKey, HashAlgorithm, SignatureOutStream);
end;

/// <summary>
/// Verifies that a digital signature is valid.
/// </summary>
/// <param name="InputString">Input string.</param>
/// <param name="XmlString">The public key to use in the hash algorithm.</param>
/// <param name="HashAlgorithm">The available hash algorithms are MD5, SHA1, SHA256, SHA384, and SHA512.</param>
/// <param name="SignatureInStream">The stream of signature.</param>
/// <returns>True if the signature is valid; otherwise, false.</returns>
procedure VerifyData(InputString: SecretText; XmlString: Text; HashAlgorithm: Enum "Hash Algorithm"; SignatureInStream: InStream): Boolean
begin
exit(CryptographyManagementImpl.VerifyData(InputString, XmlString, HashAlgorithm, SignatureInStream));
end;

/// <summary>
/// Verifies that a digital signature is valid.
/// </summary>
Expand All @@ -356,6 +463,19 @@ codeunit 1266 "Cryptography Management"
exit(CryptographyManagementImpl.VerifyData(DataInStream, XmlString, HashAlgorithm, SignatureInStream));
end;

/// <summary>
/// Verifies that a digital signature is valid.
/// </summary>
/// <param name="InputString">Input string.</param>
/// <param name="SignatureKey">The private key to use in the hash algorithm.</param>
/// <param name="HashAlgorithm">The available hash algorithms are MD5, SHA1, SHA256, SHA384, and SHA512.</param>
/// <param name="SignatureInStream">The stream of signature.</param>
/// <returns>True if the signature is valid; otherwise, false.</returns>
procedure VerifyData(InputString: SecretText; SignatureKey: Codeunit "Signature Key"; HashAlgorithm: Enum "Hash Algorithm"; SignatureInStream: InStream): Boolean
begin
exit(CryptographyManagementImpl.VerifyData(InputString, SignatureKey, HashAlgorithm, SignatureInStream));
end;

/// <summary>
/// Verifies that a digital signature is valid.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,12 @@ codeunit 1279 "Cryptography Management Impl."
exit(EncryptionIsNotActivatedQst);
end;

[NonDebuggable]
procedure GenerateHash(InputString: SecretText; HashAlgorithmType: Option MD5,SHA1,SHA256,SHA384,SHA512): Text
begin
exit(GenerateHash(InputString.Unwrap(), HashAlgorithmType));
end;

procedure GenerateHash(InputString: Text; HashAlgorithmType: Option MD5,SHA1,SHA256,SHA384,SHA512): Text
var
HashBytes: DotNet Array;
Expand All @@ -242,6 +248,13 @@ codeunit 1279 "Cryptography Management Impl."
exit(ConvertByteHashToString(HashBytes));
end;

[NonDebuggable]
procedure GenerateHashAsBase64String(InputString: SecretText; HashAlgorithmType: Option MD5,SHA1,SHA256,SHA384,SHA512): Text
begin
exit(GenerateHashAsBase64String(InputString.Unwrap(), HashAlgorithmType));
end;

[NonDebuggable]
procedure GenerateHashAsBase64String(InputString: Text; HashAlgorithmType: Option MD5,SHA1,SHA256,SHA384,SHA512): Text
var
HashBytes: DotNet Array;
Expand Down Expand Up @@ -270,6 +283,12 @@ codeunit 1279 "Cryptography Management Impl."
HashAlgorithm.Dispose();
end;

[NonDebuggable]
procedure GenerateHash(InputString: SecretText; "Key": SecretText; HashAlgorithmType: Option HMACMD5,HMACSHA1,HMACSHA256,HMACSHA384,HMACSHA512): Text
begin
exit(GenerateHash(InputString.Unwrap(), Key, HashAlgorithmType));
end;

[NonDebuggable]
procedure GenerateHash(InputString: Text; "Key": SecretText; HashAlgorithmType: Option HMACMD5,HMACSHA1,HMACSHA256,HMACSHA384,HMACSHA512): Text
TKapitan marked this conversation as resolved.
Show resolved Hide resolved
var
Expand All @@ -281,6 +300,12 @@ codeunit 1279 "Cryptography Management Impl."
exit(ConvertByteHashToString(HashBytes));
end;

[NonDebuggable]
procedure GenerateHashAsBase64String(InputString: SecretText; "Key": SecretText; HashAlgorithmType: Option HMACMD5,HMACSHA1,HMACSHA256,HMACSHA384,HMACSHA512): Text
begin
exit(GenerateHashAsBase64String(InputString.Unwrap(), Key, HashAlgorithmType));
end;

[NonDebuggable]
procedure GenerateHashAsBase64String(InputString: Text; "Key": SecretText; HashAlgorithmType: Option HMACMD5,HMACSHA1,HMACSHA256,HMACSHA384,HMACSHA512): Text
var
Expand All @@ -292,6 +317,12 @@ codeunit 1279 "Cryptography Management Impl."
exit(ConvertByteHashToBase64String(HashBytes));
end;

[NonDebuggable]
procedure GenerateBase64KeyedHashAsBase64String(InputString: SecretText; "Key": SecretText; HashAlgorithmType: Option HMACMD5,HMACSHA1,HMACSHA256,HMACSHA384,HMACSHA512): Text
begin
exit(GenerateBase64KeyedHashAsBase64String(InputString.Unwrap(), Key, HashAlgorithmType));
end;

[NonDebuggable]
procedure GenerateBase64KeyedHashAsBase64String(InputString: Text; "Key": SecretText; HashAlgorithmType: Option HMACMD5,HMACSHA1,HMACSHA256,HMACSHA384,HMACSHA512): Text
var
Expand Down Expand Up @@ -359,6 +390,12 @@ codeunit 1279 "Cryptography Management Impl."
exit(ConvertByteHashToString(HashBytes));
end;

[NonDebuggable]
procedure GenerateBase64KeyedHash(InputString: SecretText; "Key": SecretText; HashAlgorithmType: Option HMACMD5,HMACSHA1,HMACSHA256,HMACSHA384,HMACSHA512): Text
begin
exit(GenerateBase64KeyedHash(InputString.Unwrap(), Key, HashAlgorithmType));
end;

[NonDebuggable]
procedure GenerateBase64KeyedHash(InputString: Text; "Key": SecretText; HashAlgorithmType: Option HMACMD5,HMACSHA1,HMACSHA256,HMACSHA384,HMACSHA512): Text
var
Expand All @@ -370,6 +407,26 @@ codeunit 1279 "Cryptography Management Impl."
exit(ConvertByteHashToString(HashBytes));
end;

[NonDebuggable]
procedure SignData(InputString: SecretText; XmlString: SecretText; HashAlgorithm: Enum "Hash Algorithm"; SignatureOutStream: OutStream)
var
TempBlob: Codeunit "Temp Blob";
DataOutStream: OutStream;
DataInStream: InStream;
begin
if InputString.IsEmpty() then
exit;
TempBlob.CreateOutStream(DataOutStream, TextEncoding::UTF8);
TempBlob.CreateInStream(DataInStream, TextEncoding::UTF8);
DataOutStream.WriteText(InputString.Unwrap());
SignData(DataInStream, XmlString, HashAlgorithm, SignatureOutStream);
end;

procedure SignData(InputString: SecretText; SignatureKey: Codeunit "Signature Key"; HashAlgorithm: Enum "Hash Algorithm"; SignatureOutStream: OutStream)
begin
SignData(InputString, SignatureKey.ToXmlString(), HashAlgorithm, SignatureOutStream);
end;

procedure SignData(InputString: Text; XmlString: SecretText; HashAlgorithm: Enum "Hash Algorithm"; SignatureOutStream: OutStream)
var
TempBlob: Codeunit "Temp Blob";
Expand Down Expand Up @@ -406,6 +463,26 @@ codeunit 1279 "Cryptography Management Impl."
SignData(DataInStream, SignatureKey.ToXmlString(), HashAlgorithm, SignatureOutStream);
end;

[NonDebuggable]
procedure VerifyData(InputString: SecretText; XmlString: SecretText; HashAlgorithm: Enum "Hash Algorithm"; SignatureInStream: InStream): Boolean
var
TempBlob: Codeunit "Temp Blob";
DataOutStream: OutStream;
DataInStream: InStream;
begin
if InputString.IsEmpty() then
exit(false);
TempBlob.CreateOutStream(DataOutStream, TextEncoding::UTF8);
TempBlob.CreateInStream(DataInStream, TextEncoding::UTF8);
DataOutStream.WriteText(InputString.Unwrap());
exit(VerifyData(DataInStream, XmlString, HashAlgorithm, SignatureInStream));
end;

procedure VerifyData(InputString: SecretText; SignatureKey: Codeunit "Signature Key"; HashAlgorithm: Enum "Hash Algorithm"; SignatureInStream: InStream): Boolean
begin
exit(VerifyData(InputString, SignatureKey.ToXmlString(), HashAlgorithm, SignatureInStream));
end;

procedure VerifyData(InputString: Text; XmlString: SecretText; HashAlgorithm: Enum "Hash Algorithm"; SignatureInStream: InStream): Boolean
var
TempBlob: Codeunit "Temp Blob";
Expand Down
Loading