Sorcerer's IsleCode cfPassphrase / files

  1<cfcomponent extends="TestBase">
  2<cfscript>
  3
  4	function init()
  5	{
  6		super.init( argumentcollection=arguments );
  7
  8		this.Name = 'usage';
  9
 10		super.start();
 11
 12		this.test_info();
 13		this.test_hash();
 14		this.test_check();
 15
 16		return super.end();
 17	}
 18
 19
 20	function test_info()
 21	{
 22		try
 23		{
 24			var info = PassphraseInfo('$8$unknown-hash');
 25			assertFalse(1,"Expected exception to be thrown");
 26		}
 27		catch( any e )
 28		{
 29			assertEqual(e.Message,"Unknown Algorithm Signature");
 30		}
 31
 32		var info = PassphraseInfo('$2a$10$9zXu55aPNya8ek17DwCXZ.X6kExa5cK5bpGmyTBrqD1dg76rkWz4y');
 33		assertEqual(info.Algorithm,'BCrypt');
 34		assertEqual(info.Status,'Supported');
 35
 36		var info = PassphraseInfo('180:5fabd8b160f5f9225ec5569ce2f02d5a2e29a29e0b280614:d11a602ecc7830280b25cdd29b539e9e0f8438a0a43e6637');
 37		assertEqual(info.Algorithm,'PBKDF2');
 38		assertEqual(info.Status,'Supported');
 39
 40		var info = PassphraseInfo('$s0$501ff$5JQALASFiKGKdfY9Z0GYMA==$8c4aMGktdRlLIeY+erIA62fNtgb2OxJrjyhw+XeWHk4=');
 41		assertEqual(info.Algorithm,'SCrypt');
 42		assertEqual(info.Status,'Supported');
 43
 44		var info = PassphraseInfo('$1$etNnh7FA$OlM7eljE/B7F1J4XYNnk81');
 45		assertEqual(info.Algorithm,'md5crypt');
 46		assertEqual(info.Status,'Obsolete');
 47		assertEqual(info.Salt,'etNnh7FA');
 48		assertEqual(info.Hash,'OlM7eljE/B7F1J4XYNnk81');
 49
 50		var info = PassphraseInfo('$3$$8846f7eaee8fb117ad06bdd830b7586c');
 51		assertEqual(info.Algorithm,'NT-Hash');
 52		assertEqual(info.Status,'Obsolete');
 53		assertEqual(info.Hash,'8846f7eaee8fb117ad06bdd830b7586c');
 54
 55		var info = PassphraseInfo('$5$9ks3nNEqv31FX.F$gdEoLFsCRsn/WRN3wxUnzfeZLoooVlzeF4WjLomTRFD');
 56		assertEqual(info.Algorithm,'SHA-2');
 57		assertEqual(info.Version,'256');
 58		assertEqual(info.Status,'Unsupported');
 59		assertEqual(info.Rounds,'5000');
 60		assertEqual(info.Salt,'9ks3nNEqv31FX.F');
 61		assertEqual(info.Hash,'gdEoLFsCRsn/WRN3wxUnzfeZLoooVlzeF4WjLomTRFD');
 62
 63		var info = PassphraseInfo('$6$qoE2letU$wWPRl.PVczjzeMVgjiA8LLy2nOyZbf7Amj3qLIL978o18gbMySdKZ7uepq9tmMQXxyTIrS12Pln.2Q/6Xscao0');
 64		assertEqual(info.Algorithm,'SHA-2');
 65		assertEqual(info.Version,'512');
 66		assertEqual(info.Status,'Unsupported');
 67		assertEqual(info.Rounds,'5000');
 68		assertEqual(info.Salt,'qoE2letU');
 69		assertEqual(info.Hash,'wWPRl.PVczjzeMVgjiA8LLy2nOyZbf7Amj3qLIL978o18gbMySdKZ7uepq9tmMQXxyTIrS12Pln.2Q/6Xscao0');
 70
 71		var info = PassphraseInfo('$md5,rounds=5000$GUBv0xjJ$mSwgIswdjlTY0YxV7HBVm0');
 72		assertEqual(info.Algorithm,'SunMD5');
 73		assertEqual(info.Status,'Obsolete');
 74		assertEqual(info.Rounds,'5000');
 75		assertEqual(info.Salt,'GUBv0xjJ');
 76		assertEqual(info.Hash,'mSwgIswdjlTY0YxV7HBVm0');
 77
 78	}
 79
 80
 81	function test_hash()
 82	{
 83		var r = RandRange(4,6);
 84		var hash = PassphraseHash('x','bcrypt',{rounds:r});
 85		var info = PassphraseInfo(hash);
 86		assertEqual(info.Algorithm,'BCrypt');
 87		assertEqual(info.Rounds,r);
 88		assertNotEqual(hash,PassphraseHash('x','bcrypt',{rounds:r}));
 89
 90		var i = RandRange(1000,2000);
 91		var hash = PassphraseHash('x','pbkdf2',{iterations:i});
 92		var info = PassphraseInfo(hash);
 93		assertEqual(info.Algorithm,'PBKDF2');
 94		assertEqual(info.Iterations,i);
 95		assertNotEqual(hash,PassphraseHash('x','pbkdf2',{iterations:i}));
 96
 97		var c = 2^RandRange(1,3);
 98		var m = RandRange(1,3);
 99		var hash = PassphraseHash('x','scrypt',{CpuCost:c,MemoryCost:m});
100		var info = PassphraseInfo(hash);
101		assertEqual(info.Algorithm,'SCrypt');
102		assertEqual(info.CpuCost,c);
103		assertEqual(info.MemoryCost,m);
104		assertNotEqual(hash,PassphraseHash('x','scrypt',{CpuCost:c,MemoryCost:m}));
105	}
106
107
108	function test_check()
109	{
110		var hash = PassphraseHash('x','bcrypt',{rounds:4});
111		assertTrue(PassphraseCheck('x',hash));
112		assertFalse(PassphraseCheck('x ',hash));
113
114		var hash = PassphraseHash('x','pbkdf2',{iterations:1000});
115		assertTrue(PassphraseCheck('x',hash));
116		assertFalse(PassphraseCheck('x ',hash));
117
118		var hash = PassphraseHash('x','scrypt',{CpuCost:2,MemoryCost:2});
119		assertTrue(PassphraseCheck('x',hash));
120		assertFalse(PassphraseCheck('x ',hash));
121
122		try
123		{
124			PassphraseCheck(hash,'');
125			assertFalse(1,"Expected exception to be thrown");
126		}
127		catch( any e )
128		{
129			assertEqual(e.Message,"Unknown Algorithm Signature");
130		}
131	}
132
133
134</cfscript>
135</cfcomponent>