| 1 | import crypto.bcrypt |
| 2 | |
| 3 | fn test_crypto_bcrypt() { |
| 4 | bcrypt.compare_hash_and_password('123456'.bytes(), |
| 5 | r'$2a$07$MRniCPEgEQnrJmmgN.maM.kF2a/TI2PB37EQQNsUtEuINwultcHTm'.bytes())! |
| 6 | |
| 7 | hash := bcrypt.generate_from_password('password'.bytes(), 5)! |
| 8 | bcrypt.compare_hash_and_password('password'.bytes(), hash.bytes())! |
| 9 | |
| 10 | bcrypt.compare_hash_and_password('password2'.bytes(), hash.bytes()) or { |
| 11 | assert err.msg() == 'mismatched hash and password' |
| 12 | } |
| 13 | |
| 14 | hash2 := bcrypt.generate_from_password('bb'.bytes(), 5)! |
| 15 | mut hash2_must_mismatch := false |
| 16 | bcrypt.compare_hash_and_password('bbb'.bytes(), hash2.bytes()) or { |
| 17 | hash2_must_mismatch = true |
| 18 | assert err.msg() == 'mismatched hash and password' |
| 19 | } |
| 20 | |
| 21 | assert hash2_must_mismatch |
| 22 | |
| 23 | long_password := 'jvaqhblwxtoytiaglflbisdeyoieianidksglxyitwopxgrjurhjvrsuydlcguaiueliuoikabibownvfcrcaogheq' |
| 24 | assert long_password.len > 72 |
| 25 | bcrypt.generate_from_password(long_password.bytes(), 5) or { |
| 26 | assert err.msg() == 'Maximum password length is 72 bytes' |
| 27 | } |
| 28 | bcrypt.compare_hash_and_password(long_password.bytes(), hash2.bytes()) or { |
| 29 | assert err.msg() == 'Maximum password length is 72 bytes' |
| 30 | } |
| 31 | } |
| 32 | |