1 module passwd.bcrypt_test;
2 
3 @safe:
4 
5 import std.algorithm;
6 import std.range;
7 import std.utf : byCodeUnit;
8 
9 import passwd;
10 import passwd.exception;
11 import passwd.bcrypt;
12 import passwd.test;
13 
14 unittest
15 {
16 	auto salt = Bcrypt.genSalt(4);
17 	standardTests!Bcrypt(salt);
18 }
19 
20 unittest
21 {
22 	// Samples generated by using crypt(3) on an OpenBSD system
23 
24 	assert ("password".crypt("$2b$04$dyr9k14x5DORI6eLAYEJZe") == "$2b$04$dyr9k14x5DORI6eLAYEJZeiL0rwrqOVpy0lhqmjLDd8yXAfiNVtsq");
25 
26 	assert ("password".canCryptTo("$2b$04$dyr9k14x5DORI6eLAYEJZeiL0rwrqOVpy0lhqmjLDd8yXAfiNVtsq"));
27 	assert ("".canCryptTo("$2b$04$m/eWr8cQXfnVK8SHU8ihz.RxI/hUUoDk86qA4TamRj/YiqaWLjOVC"));
28 	assert ("UNICODE".canCryptTo("$2b$04$1mju8FBBostTfFtJSDb/FeiPcanv7sDZhPVt1s.m8SEtGIGDcHnUW"));
29 
30 	// Straddling the 2b password length limit
31 	assert ("password length test password length test password length test passwor".canCryptTo("$2b$04$eHwGUrzeQTixp3O/Vy3gDe93xjCLmJisjclvOjRvQG8/lKJyEhTQ2"));
32 	assert ("password length test password length test password length test password".canCryptTo("$2b$04$dpThqgF1Z6xrSz5VtEyC7eET0RHyBlQ7fuWrQg2iXnBg5WtboBetS"));
33 	assert ("password length test password length test password length test password ".canCryptTo("$2b$04$T651Z6cqQGKGdfuuSSr3.eCLMe.i6mWDIU4zYGQiuK2zjBPf9rbWi"));
34 	assert ("password length test password length test password length test password l".canCryptTo("$2b$04$eLq/WK1L5ChTUJpPn2fLBeRdUY8gbK/Zz/R2RGiPJKI68cp5XN6WW"));
35 	assert ("password length test password length test password length test password le".canCryptTo("$2b$04$gynKMkCnCB4ew89s.i1qTO8nHcLDDrdypVOmgZLsqosZAru1PKyDW"))
36 	;
37 	assert ("password length test password length test password length test password len".canCryptTo("$2b$04$T5H2/Pf5Jl1zYt8cYIB.4.n/WTXG4vxWxzEeulT3SrgQRBxb4Pz3G"));
38 
39 	// Same values for 2a at these lengths
40 	assert ("password length test password length test password length test passwor".canCryptTo("$2a$04$eHwGUrzeQTixp3O/Vy3gDe93xjCLmJisjclvOjRvQG8/lKJyEhTQ2"));
41 	assert ("password length test password length test password length test password".canCryptTo("$2a$04$dpThqgF1Z6xrSz5VtEyC7eET0RHyBlQ7fuWrQg2iXnBg5WtboBetS"));
42 	assert ("password length test password length test password length test password ".canCryptTo("$2a$04$T651Z6cqQGKGdfuuSSr3.eCLMe.i6mWDIU4zYGQiuK2zjBPf9rbWi"));
43 	assert ("password length test password length test password length test password l".canCryptTo("$2a$04$eLq/WK1L5ChTUJpPn2fLBeRdUY8gbK/Zz/R2RGiPJKI68cp5XN6WW"));
44 	assert ("password length test password length test password length test password le".canCryptTo("$2a$04$gynKMkCnCB4ew89s.i1qTO8nHcLDDrdypVOmgZLsqosZAru1PKyDW"))
45 	;
46 	assert ("password length test password length test password length test password len".canCryptTo("$2a$04$T5H2/Pf5Jl1zYt8cYIB.4.n/WTXG4vxWxzEeulT3SrgQRBxb4Pz3G"));
47 
48 	// No length wraparound at 256 for 2b
49 	assert ("password length test password length test password length test password length test password length test password length test password length test password length test password length test password length test password length test password length test passwo".canCryptTo("$2b$04$rNTvx/oRnLRy.PstU/j8puEZNnQftiK4tQlx4z2JEAkvmG8g7jvjC"));
50 
51 	// Length wraparound at 256 for 2a (historical bug compatibility)
52 	assert ("password length test password length test password length test password length test password length test password length test password length test password length test password length test password length test password length test password length test p".canCryptTo("$2a$04$Tx33aMGWL7H.xGn8jbxzTuJMYNx.kUZSm9.DJ1IrESOc77x9EH5re"));
53 	assert ("password length test password length test password length test password length test password length test password length test password length test password length test password length test password length test password length test password length test pa".canCryptTo("$2a$04$2cgY/d0TfYIuxZbX5iLClei4rYksPy6EbdCYwLVAUG.hE5S7Iuf7u"));
54 	assert ("password length test password length test password length test password length test password length test password length test password length test password length test password length test password length test password length test password length test pas".canCryptTo("$2a$04$Hv7Nry7PzFLItVH8eW2CRePmTq2Jh8BbuuMbbbGcg7TPy580DhOIC"));
55 	assert ("password length test password length test password length test password length test password length test password length test password length test password length test password length test password length test password length test password length test pass".canCryptTo("$2a$04$oM5BCVVGfqf5Mb3Td0PI6OqgZ6a5ZhWkzAG1hrn6cub5d/EIq3Ec6"));
56 	assert ("password length test password length test password length test password length test password length test password length test password length test password length test password length test password length test password length test password length test passw".canCryptTo("$2a$04$HEKj.kLAVyhxU5MW66XJ0OjGmExNdeClrjmj3TfS0p/orxatqYkDW"));
57 	assert ("password length test password length test password length test password length test password length test password length test password length test password length test password length test password length test password length test password length test passwo".canCryptTo("$2a$04$zAgYuIfkbfYmReScrWPYv.ThQcGseFid5IaD8F6CiHd6tqFycs3h6"));
58 }