-
Notifications
You must be signed in to change notification settings - Fork 4
/
Winapi.Security.Helpers.pas
76 lines (65 loc) · 3.14 KB
/
Winapi.Security.Helpers.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
{******************************************************************************}
{ }
{ WinRT Crypto Sample Applications }
{ Copyright (c) 2023 Christoph Schneider }
{ Schneider Infosystems AG, Switzerland }
{ https://github.com/SchneiderInfosystems/WinRTCrypto }
{ }
{******************************************************************************}
{ }
{ 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 Winapi.Security.Helpers;
interface
uses
System.Classes, System.SysUtils, System.Win.WinRT,
Winapi.Windows, Winapi.WinRT, Winapi.CommonTypes, Winapi.Security.Cryptography;
type
TWinRTCryptoHelpers = class
class function EncodeAsBase64(Buf: IBuffer): string;
class function DecodeFromBase64(const Base64: string): IBuffer;
class function StrToIBuffer(const Text: string): IBuffer;
class function IBufferToStr(Buf: IBuffer): string;
end;
implementation
resourcestring
rsNotApplicable = 'n/a';
class function TWinRTCryptoHelpers.EncodeAsBase64(Buf: IBuffer): string;
begin
if not assigned(Buf) then
exit(rsNotApplicable);
result := TWindowsString.HStringToString(
TCryptographicBuffer.EncodeToBase64String(Buf));
end;
class function TWinRTCryptoHelpers.DecodeFromBase64(
const Base64: string): IBuffer;
begin
if SameText(Base64, rsNotApplicable) then
exit(nil);
result := TCryptographicBuffer.DecodeFromBase64String(
TWindowsString.Create(Base64));
end;
class function TWinRTCryptoHelpers.StrToIBuffer(const Text: string): IBuffer;
var
data: TBytes;
begin
data := TEncoding.UTF8.GetBytes(Text);
result := TCryptographicBuffer.CreateFromByteArray(length(data), @data[0]);
end;
class function TWinRTCryptoHelpers.IBufferToStr(Buf: IBuffer): string;
begin
result := TCryptographicBuffer.ConvertBinaryToString(
BinaryStringEncoding.Utf8, Buf).ToString;
end;
end.