-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
ksAwsHash.pas
207 lines (172 loc) · 7.18 KB
/
ksAwsHash.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
{*******************************************************************************
* *
* ksAwsHash - Amazon Web Service Hashing Functions *
* *
* https://github.com/gmurt/ksAws *
* *
* Copyright 2020 Graham Murt *
* *
* email: graham@kernow-software.co.uk *
* *
* 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. *
* *
*******************************************************************************}
unit ksAwsHash;
interface
{$I include.inc}
{$DEFINE USE_INDY}
uses Classes {$IFDEF USE_INDY} ,IdGlobal {$ENDIF};
function URLEncode(AUrl: string): string;
function ParamEncode(AParam: string): string;
function GetHashSHA256Hex(AValue: string): string; overload;
function GetHashSHA256Hex(AValue: TStream): string; overload;
function GenerateSignature(ARequestTime: TDateTime; AStringToSign, APrivateKey, ARegionStr, AServiceName: string): string;
{$IFDEF USE_INDY}
function CalculateHMACSHA256Hex(const AValue: string; const AKey: TIdBytes): string;
function CalculateHMACSHA256(const AValue: string; const AKey: TIdBytes): TIdBytes;
{$ELSE}
//function CalculateHMACSHA256(const AValue: string; const AKey: TArray<Byte>): TArray<Byte>;
//function CalculateHMACSHA256Hex(const AValue: string; const AKey: TArray<Byte>): string;
{$ENDIF}
implementation
uses ksAwsConst, SysUtils,
{$IFDEF USE_INDY}
IdHashSHA, IdHMAC, IdHMACSHA1, IdSSLOpenSSL, IdURI
{$ELSE}
//System.Hash, System.NetEncoding
{$ENDIF}
;
const
C_UNSAFE_CHARS: array[1..27] of Byte = (Ord(' '), Ord('"'), Ord(''''), Ord(':'), Ord(';'), Ord('<'), Ord('='), Ord('>'),
Ord('@'), Ord('['), Ord(']'), Ord('^'), Ord('`'), Ord('{'), Ord('}'), Ord('|'), Ord('/'), Ord('\'), Ord('?'), Ord('#'),
Ord('&'), Ord('!'), Ord('$'), Ord('('), Ord(')'), Ord(','), Ord('~'));
{$IFDEF USE_INDY}
function ParamEncode(AParam: string): string;
var
AChar: Cardinal;
begin
Result := TIdURI.ParamsEncode(AParam);
for AChar in C_UNSAFE_CHARS do
begin
Result := StringReplace(Result, Char(AChar), '%'+Copy(ToHex([AChar]), 1, 2), [rfReplaceAll]);
end;
end;
function URLEncode(AUrl: string): string;
begin
Result := TIdURI.ParamsEncode(AUrl);
end;
function GenerateSignature(ARequestTime: TDateTime; AStringToSign, APrivateKey, ARegionStr, AServiceName: string): string;
var
ADateKeyIndy, ARegionKeyIndy, AServiceKeyIndy, ASigningKeyIndy: TIdBytes;
begin
ADateKeyIndy := CalculateHMACSHA256(FormatDateTime(C_SHORT_DATE_FORMAT, ARequestTime), IndyTextEncoding_UTF8.GetBytes('AWS4' + APrivateKey));
ARegionKeyIndy := CalculateHMACSHA256(ARegionStr, ADateKeyIndy);
AServiceKeyIndy := CalculateHMACSHA256(AServiceName, ARegionKeyIndy);
ASigningKeyIndy := CalculateHMACSHA256('aws4_request', AServiceKeyIndy);
Result := LowerCase(CalculateHMACSHA256Hex(AStringToSign, ASigningKeyIndy));
end;
function GetHashSHA256Hex(AValue: string): string;
var
ASha256: TIdHashSHA256;
begin
if TIdHashSHA256.IsAvailable then
begin
ASha256:= TIdHashSHA256.Create;
try
Result := LowerCase(ASha256.HashStringAsHex(AValue));
finally
ASha256.Free;
end;
end;
end;
function GetHashSHA256Hex(AValue: TStream): string;
var
ASha256: TIdHashSHA256;
begin
if TIdHashSHA256.IsAvailable then
begin
ASha256 := TIdHashSHA256.Create;
try
Result := LowerCase(ASha256.HashStreamAsHex(AValue));
AValue.Position := 0;
finally
ASha256.Free;
end;
end;
end;
function CalculateHMACSHA256(const AValue: string; const AKey: TIdBytes): TIdBytes;
var
hmac: TIdHMACSHA256;
begin
LoadOpenSSLLibrary;
if not TIdHashSHA256.IsAvailable then
raise Exception.Create('SHA256 hashing is not available!');
hmac := TIdHMACSHA256.Create;
try
hmac.Key := AKey;
Result := hmac.HashValue(IndyTextEncoding_UTF8.GetBytes( AValue));
finally
hmac.Free;
end;
end;
function CalculateHMACSHA256Hex(const AValue: string; const AKey: TIdBytes): string;
begin
Result := ToHex(CalculateHMACSHA256(AValue, AKey));
end;
{$ELSE}
(*
function ParamEncode(AParam: string): string;
begin
Result := TNetEncoding.URL.EncodeQuery(AParam, [Ord('"'), Ord(''''), Ord(':'), Ord(';'), Ord('<'), Ord('='), Ord('>'),
Ord('@'), Ord('['), Ord(']'), Ord('^'), Ord('`'), Ord('{'), Ord('}'), Ord('|'), Ord('/'), Ord('\'), Ord('?'), Ord('#'),
Ord('&'), Ord('!'), Ord('$'), Ord('('), Ord(')'), Ord(','), Ord('~')]);
end;
function UrlEncode(AUrl: string): string;
begin
Result := AUrl;
end;
function GenerateSignature(ARequestTime: TDateTime; AStringToSign, APrivateKey, ARegionStr, AServiceName: string): string;var
ADateKey, ARegionKey, AServiceKey, ASigningKey: TArray<Byte>;
begin
ADateKey := CalculateHMACSHA256(FormatDateTime(C_SHORT_DATE_FORMAT, ARequestTime), TEncoding.UTF8.GetBytes('AWS4' + APrivateKey));
ARegionKey := CalculateHMACSHA256(ARegionStr, ADateKey);
AServiceKey := CalculateHMACSHA256(AServiceName, ARegionKey);
ASigningKey := CalculateHMACSHA256('aws4_request', AServiceKey);
Result := CalculateHMACSHA256Hex(AStringToSign, ASigningKey);
end;
function GetHashSHA256Hex(AValue: string): string;
begin
Result := THash.DigestAsString(THashSHA2.GetHashBytes(AValue));
end;
function GetHashSHA256Hex(AValue: TStream): string;
begin
AValue.Position := 0;
Result := THash.DigestAsString(THashSHA2.GetHashBytes(AValue));
// d87edfd94fa67f662d0ab370d515266f9e09185c69802b0554ba38f6918f48b2 incorrect
// 3db2ea30b6216ce51347664d96eeef1ffdc1a746b888bbf383d5f9b948c40072
end;
function CalculateHMACSHA256(const AValue: string; const AKey: TArray<Byte>): TArray<Byte>;
begin
Result := THashSHA2.GetHMACAsBytes(AValue, AKey);
end;
function CalculateHMACSHA256Hex(const AValue: string; const AKey: TArray<Byte>): string;
begin
Result := THash.DigestAsString(CalculateHMACSHA256(AValue, AKey));
end;
*)
{$ENDIF}
initialization
{$IFDEF USE_INDY}
LoadOpenSSLLibrary;
{$ENDIF}
end.