Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: IndyTextEncoding_UTF8


This question is answered. Helpful answers available: 1. Correct answers available: 1.


Permlink Replies: 2 - Last Post: Feb 26, 2017 8:56 PM Last Post By: Fai Chung Hu
Fai Chung Hu

Posts: 2
Registered: 2/15/13
IndyTextEncoding_UTF8  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 25, 2017 2:19 AM
Greetings !

I found the following code by Remy Lebeau on implementation of HMACs in Delphi and tried it:

uses
IdGlobal, IdHashSHA, IdHMAC, IdHMACSHA1, IdSSLOpenSSL;

function CalculateHMACSHA256(const value, salt: String): String;
var
hmac: TIdHMACSHA256;
hash: TIdBytes;
begin
LoadOpenSSLLibrary;
if not TIdHashSHA256.IsAvailable then
raise Exception('SHA256 hashing is not available!');
hmac := TIdHMACSHA256.Create;
try
hmac.Key := IndyTextEncoding_UTF8.GetBytes(salt);
hash := hmac.HashValue(IndyTextEncoding_UTF8.GetBytes(value));
Result := ToHex(hash);
finally
hmac.Free;
end;
end;

However, I get a compilation error - Undeclared Indentifier: IndyTextEncoding_UTF8.

My platform:
Delphi XE2
Indy 10 v10.5.9.0

I would be most grateful if someone can tell me what I am missing. Thanks in advance !

Best, Faichung
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: IndyTextEncoding_UTF8
Helpful
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 26, 2017 4:11 PM   in response to: Fai Chung Hu in response to: Fai Chung Hu
Fai wrote:

However, I get a compilation error - Undeclared Indentifier:
IndyTextEncoding_UTF8.

Delphi XE2
Indy 10 v10.5.9.0

You are using an outdated version of Indy 10. At the time of this writing,
the current version is 10.6.2.5396. The IIdTextEncoding interface, and associated
IndyTextEncoding...() functions, were introduced in 10.6.0.0 (which shipped
in XE4):

Indy 10.6 has been released.
http://ww2.indyproject.org/Sockets/Blogs/Changelog/20130423.aspx

In the older version you have, you need to use the obsolete TIdTextEncoding
class instead (which was just an alias for Delphi's SysUtils.TEncoding class
in D2009+, and mimics the class in earlier versions):

uses
  IdGlobal, IdHashSHA, IdHMAC, IdHMACSHA1, IdSSLOpenSSL;
 
function CalculateHMACSHA256(const value, salt: String): String;
var
  hmac: TIdHMACSHA256;
  hash: TIdBytes;
begin
  LoadOpenSSLLibrary;
  if not TIdHashSHA256.IsAvailable then
    raise Exception('SHA256 hashing is not available!');
  hmac := TIdHMACSHA256.Create;
  try
    hmac.Key := TIdTextEncoding.UTF8.GetBytes(salt);
    hash := hmac.HashValue(TIdTextEncoding.UTF8.GetBytes(value));
    Result := ToHex(hash);
  finally
    hmac.Free;
  end;
end;


--
Remy Lebeau (TeamB)
Fai Chung Hu

Posts: 2
Registered: 2/15/13
Re: IndyTextEncoding_UTF8  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 26, 2017 8:56 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Thank you very much !

Remy Lebeau (TeamB) wrote:
Fai wrote:

However, I get a compilation error - Undeclared Indentifier:
IndyTextEncoding_UTF8.

Delphi XE2
Indy 10 v10.5.9.0

You are using an outdated version of Indy 10. At the time of this writing,
the current version is 10.6.2.5396. The IIdTextEncoding interface, and associated
IndyTextEncoding...() functions, were introduced in 10.6.0.0 (which shipped
in XE4):

Indy 10.6 has been released.
http://ww2.indyproject.org/Sockets/Blogs/Changelog/20130423.aspx

In the older version you have, you need to use the obsolete TIdTextEncoding
class instead (which was just an alias for Delphi's SysUtils.TEncoding class
in D2009+, and mimics the class in earlier versions):

uses
  IdGlobal, IdHashSHA, IdHMAC, IdHMACSHA1, IdSSLOpenSSL;
 
function CalculateHMACSHA256(const value, salt: String): String;
var
  hmac: TIdHMACSHA256;
  hash: TIdBytes;
begin
  LoadOpenSSLLibrary;
  if not TIdHashSHA256.IsAvailable then
    raise Exception('SHA256 hashing is not available!');
  hmac := TIdHMACSHA256.Create;
  try
    hmac.Key := TIdTextEncoding.UTF8.GetBytes(salt);
    hash := hmac.HashValue(TIdTextEncoding.UTF8.GetBytes(value));
    Result := ToHex(hash);
  finally
    hmac.Free;
  end;
end;


--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02