-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathOAuthUtils.pas
More file actions
213 lines (191 loc) · 5.13 KB
/
OAuthUtils.pas
File metadata and controls
213 lines (191 loc) · 5.13 KB
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
208
209
210
211
212
213
unit OAuthUtils;
interface
uses SysUtils,Classes,Windows,
OverbyteIcsMimeUtils,
OverbyteIcsSha1,
OverbyteIcsMD5;
type
TSpecials = set of AnsiChar;
const
URLSpecialChar: TSpecials =
[#$00..#$21,':',';','/',',','?', '<', '>', '"', '%', '{', '}', '|', '\', '^', '~', '[', ']',
'`','(',')','#', #$7F..#$FF];
UnixStartDate : TDateTime = 25569;
//работа с URL'ом
function HTTPEncode(const AStr: AnsiString): AnsiString;
function UrlDecode(const Url : String) : String;
function UrlEncode(const S : String) : String;
//шифрование данных
function EncodeTriplet(const Value: AnsiString; Delimiter: AnsiChar;
Specials: TSpecials): AnsiString;
function Base64Encode_(const Input: TBytes): string;
function EncryptHMACSha1(Input, AKey: string): TBytes;
function MD5(const Value:string):string;
//вспомогательные функции
function DateTimeToUnix(ConvDate: TDateTime): Longint;
function XDigit(Ch : Char) : Integer;
function IsXDigit(Ch : Char) : Boolean;
function htoin(Value : PChar; Len : Integer) : Integer;
function htoi2(Value : PChar) : Integer;
implementation
function MD5(const Value:string):string;
begin
Result:=StrMD5(Value);
end;
function HTTPEncode(const AStr: AnsiString): AnsiString;
const
NoConversion = ['A'..'Z','a'..'z','*','@','.','_','-',
'0'..'9','$','!','''','(',')'];
var
Sp, Rp: PAnsiChar;
begin
SetLength(Result, Length(AStr) * 3);
Sp := PAnsiChar(AStr);
Rp := PAnsiChar(Result);
while Sp^ <> #0 do
begin
if Sp^ in NoConversion then
Rp^ := Sp^
else
if Sp^ = ' ' then
Rp^ := '+'
else
begin
FormatBuf(Rp^, 3, AnsiString('%%%.2x'), 6, [Ord(Sp^)]);
Inc(Rp,2);
end;
Inc(Rp);
Inc(Sp);
end;
SetLength(Result, Rp - PAnsiChar(Result));
end;
function EncodeTriplet(const Value: AnsiString; Delimiter: AnsiChar;
Specials: TSpecials): AnsiString;
var
n, l: Integer;
s: AnsiString;
c: AnsiChar;
begin
SetLength(Result, Length(Value) * 3);
l := 1;
for n := 1 to Length(Value) do
begin
c := Value[n];
if c in Specials then
begin
Result[l] := Delimiter;
Inc(l);
s := IntToHex(Ord(c), 2);
Result[l] := s[1];
Inc(l);
Result[l] := s[2];
Inc(l);
end
else
begin
Result[l] := c;
Inc(l);
end;
end;
Dec(l);
SetLength(Result, l);
end;
function Base64Encode_(const Input: TBytes): string;
begin
Result:=Base64Encode(StringOf(Input));
end;
function DateTimeToUnix(ConvDate: TDateTime): Longint;
var
x: double;
lTimeZone: TTimeZoneInformation;
begin
GetTimeZoneInformation(lTimeZone);
ConvDate := ConvDate + (lTimeZone.Bias / 1440);
x := (ConvDate - UnixStartDate) * 86400;
Result := Trunc(x);
end;
function XDigit(Ch : Char) : Integer;
begin
if (Ch >= '0') and (Ch <= '9') then
Result := Ord(Ch) - Ord('0')
else
Result := (Ord(Ch) and 15) + 9;
end;
function IsXDigit(Ch : Char) : Boolean;
begin
Result := ((Ch >= '0') and (Ch <= '9')) or
((Ch >= 'a') and (Ch <= 'f')) or
((Ch >= 'A') and (Ch <= 'F'));
end;
function htoin(Value : PChar; Len : Integer) : Integer;
var
I : Integer;
begin
Result := 0;
I := 0;
while (I < Len) and (Value[I] = ' ') do
I := I + 1;
while (I < len) and (IsXDigit(Value[I])) do begin
Result := Result * 16 + XDigit(Value[I]);
I := I + 1;
end;
end;
function EncryptHMACSha1(Input, AKey: string): TBytes;
begin
Result:=BytesOf(HMAC_SHA1_EX(Input,AKey));
end;
function htoi2(Value : PChar) : Integer;
begin
Result := htoin(Value, 2);
end;
function UrlEncode(const S : String) : String;
var
I : Integer;
Ch : Char;
begin
Result := '';
for I := 1 to Length(S) do begin
Ch := S[I];
if ((Ch >= '0') and (Ch <= '9')) or
((Ch >= 'a') and (Ch <= 'z')) or
((Ch >= 'A') and (Ch <= 'Z')) or
(Ch = '.') or (Ch = '-') or (Ch = '_') or (Ch = '~')then
Result := Result + Ch
else
Result := Result + '%' + IntToHex(Ord(Ch), 2);
end;
end;
function UrlDecode(const Url : String) : String;
var
I, J, K, L : Integer;
begin
Result := Url;
L := Length(Result);
I := 1;
K := 1;
while TRUE do begin
J := I;
while (J <= Length(Result)) and (Result[J] <> '%') do begin
if J <> K then
Result[K] := Result[J];
Inc(J);
Inc(K);
end;
if J > Length(Result) then
break; { End of string }
if J > (Length(Result) - 2) then begin
while J <= Length(Result) do begin
Result[K] := Result[J];
Inc(J);
Inc(K);
end;
break;
end;
Result[K] := Char(htoi2(@Result[J + 1]));
Inc(K);
I := J + 3;
Dec(L, 2);
end;
SetLength(Result, L);
end;
end.