Skip to content

std.crypto: add pem.zig #23399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed

std.crypto: add pem.zig #23399

wants to merge 5 commits into from

Conversation

deatil
Copy link
Contributor

@deatil deatil commented Mar 29, 2025

No description provided.


const Self = @This();

/// init
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid writing redundant comments.

Comment on lines 350 to 377
fn isSpace(r: u8) bool {
return switch(r) {
'\t', '\n', '\r', ' ', 0x85, 0xA0 => true,
else => false,
};
}

fn trimSpace(s: []const u8) []const u8 {
var start: usize = 0;
while (start < s.len) : (start += 1) {
if (!isSpace(s[start])) {
break;
}
}

var stop = s.len - 1;
while (stop > start) : (stop -= 1) {
if (!isSpace(s[stop])) {
break;
}
}

if (start == stop) {
return "";
}

return s[start..(stop+1)];
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These functions already exist in std.ascii and std.mem

Comment on lines 266 to 273
fn contains(data: []const u8, sep: []const u8) bool {
const i = mem.indexOf(u8, data, sep);
if (i != null) {
return true;
}

return false;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not directly use mem.indexOf?

Comment on lines 318 to 323
}

fn hasSuffix(rest: []const u8, needle: []const u8) bool {
return rest.len > needle.len and mem.eql(u8, rest[rest.len-needle.len..], needle);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mem.startsWith/mem.endsWith

@@ -0,0 +1,540 @@
const std = @import("std");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whole implementation is inefficient.
e.g. There are multiple allocations that could be combined into one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alichraghi How do change it? I think what it is right.


const pem_line_length = 64;

fn writeHeader(writer: anytype, k: []const u8, v: []const u8) !void {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'd want to rename this to fn appendHeader(list: *std.ArrayList(u8), ...) because a writer (at least as a convention) doesn't have a appendSlice method

"ILwpnZ1izL4MlI9eCSHhVQBHEp2uQdXJB+d5Byg=\n" ++
"-----END CERTIFICATE-----\n";

const alloc = std.heap.page_allocator;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use std.testing.allocator instead

const buffer = try allocator.alloc(u8, bytes_len);

const banse64_encoded = base64.standard.Encoder.encode(buffer, b.bytes);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Encoding/decoding should really be done in constant time.

We currently don't have hex/base64 codecs suitable for secrets, but that should probably be the first thing to add before adding PEM encoding.

https://github.com/jedisct1/libsodium/blob/master/src/libsodium/sodium/codecs.c can easily be ported to Zig.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm working on this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pem.zig use the std.base64.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noting that #23420 has been merged in case you want to rebase and make use of it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@deatil deatil closed this Apr 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants