|
| 1 | +{***************************************************************************** |
| 2 | + The DEC team (see file NOTICE.txt) licenses this file |
| 3 | + to you under the Apache License, Version 2.0 (the |
| 4 | + "License"); you may not use this file except in compliance |
| 5 | + with the license. A copy of this licence is found in the root directory of |
| 6 | + this project in the file LICENCE.txt or alternatively at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, |
| 11 | + software distributed under the License is distributed on an |
| 12 | + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 13 | + KIND, either express or implied. See the License for the |
| 14 | + specific language governing permissions and limitations |
| 15 | + under the License. |
| 16 | +*****************************************************************************} |
| 17 | + |
| 18 | +/// <summary> |
| 19 | +/// Some demonstrations of how to use the password hashing classes |
| 20 | +/// </summary> |
| 21 | +program Password_Console; |
| 22 | + |
| 23 | +{$APPTYPE CONSOLE} |
| 24 | + |
| 25 | +{$R *.res} |
| 26 | + |
| 27 | +uses |
| 28 | + System.SysUtils, |
| 29 | + DECHashAuthentication, |
| 30 | + DECHash, |
| 31 | + DECFormat; |
| 32 | + |
| 33 | +var |
| 34 | + HashInst : THash_BCrypt; |
| 35 | + Result : Boolean; |
| 36 | + Password : string; |
| 37 | + |
| 38 | +begin |
| 39 | + HashInst := THash_BCrypt.Create; |
| 40 | + try |
| 41 | + try |
| 42 | + // manually calculate the password hash using the BCrypt algorithm |
| 43 | + |
| 44 | + // Cost defines how many rounds are used, the higher the stronger. |
| 45 | + // See MinCost and MaxCost methods as well. |
| 46 | + HashInst.Cost := 6; |
| 47 | + // Salt for BCrypt must always be 16 byte long. |
| 48 | + HashInst.Salt := [$2a, $1f, $1d, $c7, $0a, $3d, $14, $79, |
| 49 | + $56, $a4, $6f, $eb, $e3, $01, $60, $17]; |
| 50 | + // Calculate the hash for password 'abc' and display it in hexadecimal |
| 51 | + WriteLn('Hash for password abc is:'); |
| 52 | + WriteLn(HashInst.CalcString('abc', TFormat_HEXL)); |
| 53 | + WriteLn; |
| 54 | + |
| 55 | + // Generate a Crypt/BSD style password entry. The BSD operating system |
| 56 | + // stores his password records in this format. More information about the |
| 57 | + // format in the XMLDOC of the TDECHash_Authentication and THash_BCrypt |
| 58 | + // classes. |
| 59 | + // The formatting class TFormat_BCryptBSD must be passed here, as this |
| 60 | + // avoids dragging the TDECFormat unit into DECHashAuthentication and |
| 61 | + // DECHash units just for a case not everybody needs. The right output |
| 62 | + // is: '$2a$06$If6bvum7DFjUnE9p2uDeDu0YHzrHM6tf.iqN8.yx.jNN1ILEf7h0i' |
| 63 | + WriteLn('Crypt/BSD data for password abc is:'); |
| 64 | + WriteLn(HashInst.GetDigestInCryptFormat('abc', |
| 65 | + '6', |
| 66 | + 'If6bvum7DFjUnE9p2uDeDu', |
| 67 | + false, |
| 68 | + TFormat_BCryptBSD)); |
| 69 | + WriteLn; |
| 70 | + |
| 71 | + // Check some entered password |
| 72 | + WriteLn('Enter correct password to continue (correct value is: GoOn!):'); |
| 73 | + |
| 74 | + repeat |
| 75 | + ReadLn(Password); |
| 76 | + // the data against which the entered password is compared is given |
| 77 | + // in Crypt/BSD style format, esp. in the BCrypt variant of that format |
| 78 | + Result := HashInst.IsValidPassword(Password, |
| 79 | + '$2a$06$If6bvum7DFjUnE9p2uDeDuJZX' + |
| 80 | + '1LXp30kMOn/QEnf4laWZvcLxd0iK', |
| 81 | + TFormat_BCryptBSD); |
| 82 | + if not Result then |
| 83 | + WriteLn('Entered password is wrong!'); |
| 84 | + until Result; |
| 85 | + |
| 86 | + WriteLn('Entered password is correct!'); |
| 87 | + except |
| 88 | + on E: Exception do |
| 89 | + Writeln(E.ClassName, ': ', E.Message); |
| 90 | + end; |
| 91 | + finally |
| 92 | + HashInst.Free; |
| 93 | + end; |
| 94 | + |
| 95 | + WriteLn; |
| 96 | + WriteLn('Press enter to quit'); |
| 97 | + ReadLn; |
| 98 | +end. |
0 commit comments