| ... | ... | @@ -417,9 +417,11 @@ const PhcFormatHasher = struct { |
| 417 | 417 | password: []const u8, |
| 418 | 418 | params: Params, |
| 419 | 419 | buf: []u8, |
| 420 | | /// Filled with cryptographically secure entropy. |
| 421 | | salt: []const u8, |
| 420 | io: std.Io, |
| 422 | 421 | ) HasherError![]const u8 { |
| 422 | var salt: [default_salt_len]u8 = undefined; |
| 423 | io.random(&salt); |
| 424 | |
| 423 | 425 | var hash: [default_hash_len]u8 = undefined; |
| 424 | 426 | try kdf(allocator, &hash, password, &salt, params); |
| 425 | 427 | |
| ... | ... | @@ -465,9 +467,10 @@ const CryptFormatHasher = struct { |
| 465 | 467 | password: []const u8, |
| 466 | 468 | params: Params, |
| 467 | 469 | buf: []u8, |
| 468 | | /// Filled with cryptographically secure entropy. |
| 469 | | salt_bin: []const u8, |
| 470 | io: std.Io, |
| 470 | 471 | ) HasherError![]const u8 { |
| 472 | var salt_bin: [default_salt_len]u8 = undefined; |
| 473 | io.random(&salt_bin); |
| 471 | 474 | const salt = crypt_format.saltFromBin(salt_bin.len, salt_bin); |
| 472 | 475 | |
| 473 | 476 | var hash: [default_hash_len]u8 = undefined; |
| ... | ... | @@ -514,11 +517,12 @@ pub fn strHash( |
| 514 | 517 | password: []const u8, |
| 515 | 518 | options: HashOptions, |
| 516 | 519 | out: []u8, |
| 520 | io: std.Io, |
| 517 | 521 | ) Error![]const u8 { |
| 518 | 522 | const allocator = options.allocator orelse return Error.AllocatorRequired; |
| 519 | 523 | switch (options.encoding) { |
| 520 | | .phc => return PhcFormatHasher.create(allocator, password, options.params, out), |
| 521 | | .crypt => return CryptFormatHasher.create(allocator, password, options.params, out), |
| 524 | .phc => return PhcFormatHasher.create(allocator, password, options.params, out, io), |
| 525 | .crypt => return CryptFormatHasher.create(allocator, password, options.params, out, io), |
| 522 | 526 | } |
| 523 | 527 | } |
| 524 | 528 | |
| ... | ... | @@ -630,6 +634,7 @@ test "password hashing (crypt format)" { |
| 630 | 634 | if (!run_long_tests) return error.SkipZigTest; |
| 631 | 635 | |
| 632 | 636 | const alloc = std.testing.allocator; |
| 637 | const io = std.testing.io; |
| 633 | 638 | |
| 634 | 639 | const str = "$7$A6....1....TrXs5Zk6s8sWHpQgWDIXTR8kUU3s6Jc3s.DtdS8M2i4$a4ik5hGDN7foMuHOW.cp.CtX01UyCeO0.JAG.AHPpx5"; |
| 635 | 640 | const password = "Y0!?iQa9M%5ekffW(`"; |
| ... | ... | @@ -637,7 +642,7 @@ test "password hashing (crypt format)" { |
| 637 | 642 | |
| 638 | 643 | const params = Params.interactive; |
| 639 | 644 | var buf: [CryptFormatHasher.pwhash_str_length]u8 = undefined; |
| 640 | | const str2 = try CryptFormatHasher.create(alloc, password, params, &buf); |
| 645 | const str2 = try CryptFormatHasher.create(alloc, password, params, &buf, io); |
| 641 | 646 | try CryptFormatHasher.verify(alloc, str2, password); |
| 642 | 647 | } |
| 643 | 648 | |
| ... | ... | @@ -645,6 +650,7 @@ test "strHash and strVerify" { |
| 645 | 650 | if (!run_long_tests) return error.SkipZigTest; |
| 646 | 651 | |
| 647 | 652 | const alloc = std.testing.allocator; |
| 653 | const io = std.testing.io; |
| 648 | 654 | |
| 649 | 655 | const password = "testpass"; |
| 650 | 656 | const params = Params.interactive; |
| ... | ... | @@ -656,6 +662,7 @@ test "strHash and strVerify" { |
| 656 | 662 | password, |
| 657 | 663 | .{ .allocator = alloc, .params = params, .encoding = .crypt }, |
| 658 | 664 | &buf, |
| 665 | io, |
| 659 | 666 | ); |
| 660 | 667 | try strVerify(str, password, verify_options); |
| 661 | 668 | } |
| ... | ... | @@ -664,6 +671,7 @@ test "strHash and strVerify" { |
| 664 | 671 | password, |
| 665 | 672 | .{ .allocator = alloc, .params = params, .encoding = .phc }, |
| 666 | 673 | &buf, |
| 674 | io, |
| 667 | 675 | ); |
| 668 | 676 | try strVerify(str, password, verify_options); |
| 669 | 677 | } |