Skip to content

Commit

Permalink
Merge pull request #1 from byte911/main
Browse files Browse the repository at this point in the history
Add 02-01 Sha256
  • Loading branch information
jiacai2050 authored Dec 12, 2023
2 parents b596ab4 + cac026e commit 8dee8eb
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 3 deletions.
8 changes: 5 additions & 3 deletions examples/src/01-01.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ const fs = std.fs;
const print = std.debug.print;

pub fn main() !void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const allocator = arena.allocator();
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();

const file = try fs.cwd().openFile("build.zig", .{});
defer file.close();

const rdr = file.reader();
var line_no: usize = 0;
while (try rdr.readUntilDelimiterOrEofAlloc(allocator, '\n', 4096)) |line| {
defer allocator.free(line);

line_no += 1;
print("{d}--{s}\n", .{ line_no, line });
}
Expand Down
30 changes: 30 additions & 0 deletions examples/src/02-01.zig
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)});
}
2 changes: 2 additions & 0 deletions src/01-01-read-file-line-by-line.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ There is a Reader type in Zig, which provides various methods to read file.
const rdr = file.reader();
var line_no: usize = 0;
while (try rdr.readUntilDelimiterOrEofAlloc(allocator, '\n', 4096)) |line| {
defer allocator.free(line);
line_no += 1;
print("{d}--{s}\n", .{ line_no, line });
}
Expand Down
36 changes: 36 additions & 0 deletions src/02-01-sha-digest.md
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)});
}
```
3 changes: 3 additions & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@
- [File System]()
- [Read file line by line](./01-01-read-file-line-by-line.md)
- [Mmap file](./01-02-mmap-file.md)

- [Cryptography]()
- [Calculate SHA-256 digest of a file](./02-01-sha-digest.md)

0 comments on commit 8dee8eb

Please sign in to comment.