-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from byte911/main
Add 02-01 Sha256
- Loading branch information
Showing
5 changed files
with
76 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const std = @import("std"); | ||
const fs = std.fs; | ||
const print = std.debug.print; | ||
const Sha256 = std.crypto.hash.sha2.Sha256; | ||
|
||
fn sha256_digest( | ||
allocator: std.mem.Allocator, | ||
file: fs.File, | ||
) ![Sha256.digest_length]u8 { | ||
const md = try file.metadata(); | ||
const size = md.size(); | ||
const bytes = try file.reader().readAllAlloc(allocator, size); | ||
defer allocator.free(bytes); | ||
|
||
var out: [Sha256.digest_length]u8 = undefined; | ||
Sha256.hash(bytes, &out, .{}); | ||
return out; | ||
} | ||
|
||
pub fn main() !void { | ||
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | ||
defer _ = gpa.deinit(); | ||
const allocator = gpa.allocator(); | ||
|
||
const file = try fs.cwd().openFile("build.zig", .{}); | ||
defer file.close(); | ||
|
||
const digest = try sha256_digest(allocator, file); | ||
print("SHA-256 digest is {s}", .{std.fmt.fmtSliceHexLower(&digest)}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
## Calculate SHA-256 digest of a file | ||
|
||
There are many crypto algorithm implementations in std, `sha256`, `md5` are supported out of box. | ||
|
||
```zig | ||
const std = @import("std"); | ||
const fs = std.fs; | ||
const print = std.debug.print; | ||
const Sha256 = std.crypto.hash.sha2.Sha256; | ||
fn sha256_digest( | ||
allocator: std.mem.Allocator, | ||
file: fs.File, | ||
) ![Sha256.digest_length]u8 { | ||
const md = try file.metadata(); | ||
const size = md.size(); | ||
const bytes = try file.reader().readAllAlloc(allocator, size); | ||
defer allocator.free(bytes); | ||
var out: [Sha256.digest_length]u8 = undefined; | ||
Sha256.hash(bytes, &out, .{}); | ||
return out; | ||
} | ||
pub fn main() !void { | ||
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | ||
defer _ = gpa.deinit(); | ||
const allocator = gpa.allocator(); | ||
const file = try fs.cwd().openFile("build.zig", .{}); | ||
defer file.close(); | ||
const digest = try sha256_digest(allocator, file); | ||
print("SHA-256 digest is {s}", .{std.fmt.fmtSliceHexLower(&digest)}); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters