2016-06-22 17:49:16 +02:00
|
|
|
// ***************************************************************************
|
|
|
|
//
|
|
|
|
// Delphi MVC Framework
|
|
|
|
//
|
2017-01-05 12:44:34 +01:00
|
|
|
// Copyright (c) 2010-2017 Daniele Teti and the DMVCFramework Team
|
2016-06-22 17:49:16 +02:00
|
|
|
//
|
|
|
|
// https://github.com/danieleteti/delphimvcframework
|
|
|
|
//
|
|
|
|
// ***************************************************************************
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
// *************************************************************************** }
|
|
|
|
|
2016-05-18 18:21:43 +02:00
|
|
|
unit MVCFramework.HMAC;
|
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
{$I dmvcframework.inc}
|
|
|
|
|
2016-05-18 18:21:43 +02:00
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
System.SysUtils,
|
2017-03-20 19:08:01 +01:00
|
|
|
System.Generics.Collections,
|
2016-05-18 18:21:43 +02:00
|
|
|
EncdDecd,
|
|
|
|
IdHMAC;
|
|
|
|
|
|
|
|
type
|
|
|
|
|
2017-03-20 19:08:01 +01:00
|
|
|
EMVCHMACException = class(Exception)
|
|
|
|
private
|
|
|
|
{ private declarations }
|
|
|
|
protected
|
|
|
|
{ protected declarations }
|
|
|
|
public
|
|
|
|
{ public declarations }
|
2016-05-18 18:21:43 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
THMACClass = class of TIdHMAC;
|
|
|
|
|
2017-03-20 19:08:01 +01:00
|
|
|
function HMAC(const AAlgorithm: String; const AInput, AKey: string): TBytes;
|
|
|
|
procedure RegisterHMACAlgorithm(const AAlgorithm: String; AClazz: THMACClass);
|
|
|
|
procedure UnRegisterHMACAlgorithm(const AAlgorithm: String);
|
2016-05-18 18:21:43 +02:00
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
2017-03-20 19:08:01 +01:00
|
|
|
IdSSLOpenSSL,
|
|
|
|
IdHash,
|
|
|
|
IdGlobal,
|
|
|
|
IdHMACMD5,
|
|
|
|
IdHMACSHA1;
|
2016-05-18 18:21:43 +02:00
|
|
|
|
|
|
|
var
|
|
|
|
GHMACRegistry: TDictionary<string, THMACClass>;
|
|
|
|
|
2017-03-20 19:08:01 +01:00
|
|
|
function HMAC(const AAlgorithm: String; const AInput, AKey: string): TBytes;
|
2016-05-18 18:21:43 +02:00
|
|
|
var
|
2017-03-20 19:08:01 +01:00
|
|
|
LHMAC: TIdHMAC;
|
2016-05-18 18:21:43 +02:00
|
|
|
begin
|
2017-03-20 19:08:01 +01:00
|
|
|
if not GHMACRegistry.ContainsKey(AAlgorithm) then
|
|
|
|
raise EMVCHMACException.CreateFmt('Unknown HMAC algorithm [%s]', [AAlgorithm]);
|
2016-05-18 18:21:43 +02:00
|
|
|
|
2017-03-20 19:08:01 +01:00
|
|
|
LHMAC := GHMACRegistry[AAlgorithm].Create;
|
2016-05-18 18:21:43 +02:00
|
|
|
try
|
2017-03-20 19:08:01 +01:00
|
|
|
LHMAC.Key := ToBytes(AKey);
|
|
|
|
Result := TBytes(LHMAC.HashValue(ToBytes(AInput)));
|
2016-05-18 18:21:43 +02:00
|
|
|
finally
|
2017-03-20 19:08:01 +01:00
|
|
|
LHMAC.Free;
|
2016-05-18 18:21:43 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2017-03-20 19:08:01 +01:00
|
|
|
procedure RegisterHMACAlgorithm(const AAlgorithm: String; AClazz: THMACClass);
|
2016-05-18 18:21:43 +02:00
|
|
|
begin
|
2017-03-20 19:08:01 +01:00
|
|
|
if GHMACRegistry.ContainsKey(AAlgorithm) then
|
2016-05-18 18:21:43 +02:00
|
|
|
raise EMVCHMACException.Create('Algorithm already registered');
|
2017-03-20 19:08:01 +01:00
|
|
|
GHMACRegistry.Add(AAlgorithm, AClazz);
|
2016-05-18 18:21:43 +02:00
|
|
|
end;
|
|
|
|
|
2017-03-20 19:08:01 +01:00
|
|
|
procedure UnRegisterHMACAlgorithm(const AAlgorithm: String);
|
2016-05-18 18:21:43 +02:00
|
|
|
begin
|
2017-03-20 19:08:01 +01:00
|
|
|
GHMACRegistry.Remove(AAlgorithm);
|
2016-05-18 18:21:43 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
initialization
|
|
|
|
|
|
|
|
Assert(IdSSLOpenSSL.LoadOpenSSLLibrary, 'HMAC requires OpenSSL libraries');
|
|
|
|
|
|
|
|
GHMACRegistry := TDictionary<string, THMACClass>.Create;
|
|
|
|
|
2017-03-20 19:08:01 +01:00
|
|
|
// registering based on hash function
|
2016-05-18 18:21:43 +02:00
|
|
|
RegisterHMACAlgorithm('md5', TIdHMACMD5);
|
|
|
|
RegisterHMACAlgorithm('sha1', TIdHMACSHA1);
|
|
|
|
RegisterHMACAlgorithm('sha224', TIdHMACSHA224);
|
|
|
|
RegisterHMACAlgorithm('sha256', TIdHMACSHA256);
|
|
|
|
RegisterHMACAlgorithm('sha384', TIdHMACSHA384);
|
|
|
|
RegisterHMACAlgorithm('sha512', TIdHMACSHA512);
|
|
|
|
|
2017-03-20 19:08:01 +01:00
|
|
|
// the same using the JWT naming
|
2016-05-18 18:21:43 +02:00
|
|
|
RegisterHMACAlgorithm('HS256', TIdHMACSHA256);
|
|
|
|
RegisterHMACAlgorithm('HS384', TIdHMACSHA384);
|
|
|
|
RegisterHMACAlgorithm('HS512', TIdHMACSHA512);
|
|
|
|
|
|
|
|
finalization
|
|
|
|
|
|
|
|
GHMACRegistry.Free;
|
|
|
|
|
|
|
|
end.
|