Last active
July 14, 2016 16:25
-
-
Save svanas/aa20392d21a1f15f579c72f5f20ca599 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{******************************************************************************} | |
{* ISecureString *} | |
{* *} | |
{* Created by Stefan van As <dutchdelphidude@gmail.com> *} | |
{* *} | |
{* DISCLAIMER *} | |
{* *} | |
{* This software is provided 'as is' with no explicit or implied warranties *} | |
{* in respect of its properties, including, but not limited to, correctness *} | |
{* and/or fitness for purpose. *} | |
{******************************************************************************} | |
unit SecureString; | |
interface | |
type | |
ISecureString = interface | |
function Data: string; | |
function Length: Integer; | |
end; | |
function NewSecureString(const S: string): ISecureString; | |
implementation | |
uses | |
Windows; | |
{ TSecureString } | |
type | |
TSecureString = class(TInterfacedObject, ISecureString) | |
strict private | |
FData: string; | |
public | |
constructor Create(const Value: string); | |
destructor Destroy; override; | |
function Data: string; | |
function Length: Integer; | |
end; | |
constructor TSecureString.Create(const Value: string); | |
begin | |
inherited Create; | |
SetString(FData, PChar(Value), System.Length(Value)); | |
end; | |
destructor TSecureString.Destroy; | |
var | |
I: Integer; | |
begin | |
if System.Length(FData) > 0 then | |
begin | |
I := PInteger(PByte(FData) - 8)^; | |
if (I > -1) and (I < 2) then | |
ZeroMemory(Pointer(FData), System.Length(FData) * SizeOf(Char)); | |
end; | |
inherited Destroy; | |
end; | |
function TSecureString.Data: string; | |
begin | |
Result := FData; | |
end; | |
function TSecureString.Length: Integer; | |
begin | |
Result := System.Length(FData); | |
end; | |
function NewSecureString(const S: string): ISecureString; | |
var | |
I: Integer; | |
begin | |
Result := TSecureString.Create(S); | |
if System.Length(S) > 0 then | |
begin | |
I := PInteger(PByte(S) - 8)^; | |
if (I > -1) and (I < 2) then | |
ZeroMemory(Pointer(S), System.Length(S) * SizeOf(Char)); | |
end; | |
end; | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment