跳转到内容

std.crypto.codecs.asn1.der

标准库Zig 0.17.0官方原文
源码std/crypto/codecs/asn1/der.zig

第 1 行

X.690 和 X.691 定义的确定编码规则。

基本编码规则(BER)的子集,为实现规范形式 而消除灵活性。用于 PKI。

std.crypto.codecs.asn1.der.asn1

别名

第 6 行

别名目标 std.crypto.codecs.asn1

std.crypto.codecs.asn1.der.asn1_tags

常量

第 83 行

std.crypto.codecs.asn1.der.decode

函数

第 11 行

pub fn decode(comptime T: type, encoded: []const u8) !T

参数

T: type
encoded: []const u8

用法示例

test decode {
    // https://lapo.it/asn1js/#MAgGAyoDBAIBBA
    const Value = struct { a: asn1.Oid, b: i32 };
    const test_case = .{
        .value = Value{ .a = asn1.Oid.fromDotComptime("1.2.3.4"), .b = 4 },
        .encoded = &[_]u8{ 0x30, 0x08, 0x06, 0x03, 0x2A, 0x03, 0x04, 0x02, 0x01, 0x04 },
    };
    const decoded = try decode(Value, test_case.encoded);

    try std.testing.expectEqualDeep(test_case.value, decoded);
}

std.crypto.codecs.asn1.der.Decoder

别名

第 8 行

别名目标 std.crypto.codecs.asn1.der.Decoder

std.crypto.codecs.asn1.der.encode

函数

第 19 行

调用方拥有返回内存的所有权。

pub fn encode(allocator: std.mem.Allocator, value: anytype) ![]u8

参数

allocator: std.mem.Allocator

用法示例

test encode {
    // https://lapo.it/asn1js/#MAgGAyoDBAIBBA
    const Value = struct { a: asn1.Oid, b: i32 };
    const test_case = .{
        .value = Value{ .a = asn1.Oid.fromDotComptime("1.2.3.4"), .b = 4 },
        .encoded = &[_]u8{ 0x30, 0x08, 0x06, 0x03, 0x2A, 0x03, 0x04, 0x02, 0x01, 0x04 },
    };
    const allocator = std.testing.allocator;
    const actual = try encode(allocator, test_case.value);
    defer allocator.free(actual);

    try std.testing.expectEqualSlices(u8, test_case.encoded, actual);
}

std.crypto.codecs.asn1.der.Encoder

别名

第 9 行

别名目标 std.crypto.codecs.asn1.der.Encoder

std.crypto.codecs.asn1.der.std

别名

第 5 行

别名目标 std

源码

std/crypto/codecs/asn1/der.zig

//! X.690 和 X.691 定义的确定编码规则。
//!
//! 基本编码规则(BER)的子集,为实现规范形式
//! 而消除灵活性。用于 PKI。
const std = @import("std");
const asn1 = @import("../asn1.zig");

pub const Decoder = @import("der/Decoder.zig");
pub const Encoder = @import("der/Encoder.zig");

pub fn decode(comptime T: type, encoded: []const u8) !T {
    var decoder = Decoder{ .bytes = encoded };
    const res = try decoder.any(T);
    std.debug.assert(decoder.index == encoded.len);
    return res;
}

/// 调用方拥有返回内存的所有权。
pub fn encode(allocator: std.mem.Allocator, value: anytype) ![]u8 {
    var encoder = Encoder.init(allocator);
    defer encoder.deinit();
    try encoder.any(value);
    return try encoder.buffer.toOwnedSlice();
}

test encode {
    // https://lapo.it/asn1js/#MAgGAyoDBAIBBA
    const Value = struct { a: asn1.Oid, b: i32 };
    const test_case = .{
        .value = Value{ .a = asn1.Oid.fromDotComptime("1.2.3.4"), .b = 4 },
        .encoded = &[_]u8{ 0x30, 0x08, 0x06, 0x03, 0x2A, 0x03, 0x04, 0x02, 0x01, 0x04 },
    };
    const allocator = std.testing.allocator;
    const actual = try encode(allocator, test_case.value);
    defer allocator.free(actual);

    try std.testing.expectEqualSlices(u8, test_case.encoded, actual);
}

test decode {
    // https://lapo.it/asn1js/#MAgGAyoDBAIBBA
    const Value = struct { a: asn1.Oid, b: i32 };
    const test_case = .{
        .value = Value{ .a = asn1.Oid.fromDotComptime("1.2.3.4"), .b = 4 },
        .encoded = &[_]u8{ 0x30, 0x08, 0x06, 0x03, 0x2A, 0x03, 0x04, 0x02, 0x01, 0x04 },
    };
    const decoded = try decode(Value, test_case.encoded);

    try std.testing.expectEqualDeep(test_case.value, decoded);
}

test "integer round trip across signed and unsigned boundaries" {
    const allocator = std.testing.allocator;
    inline for (.{ u8, u16, u32, i8, i16, i32 }) |T| {
        const cases = comptime blk: {
            const min = std.math.minInt(T);
            const max = std.math.maxInt(T);
            break :blk [_]T{ 0, 1, max, min, @divTrunc(max, 2), @divTrunc(min, 2) };
        };
        for (cases) |value| {
            const buf = try encode(allocator, value);
            defer allocator.free(buf);
            const decoded = try decode(T, buf);
            try std.testing.expectEqual(value, decoded);
        }
    }
}

test "encode skips null optional fields" {
    const Value = struct { a: ?u8, b: u8 };
    const allocator = std.testing.allocator;
    const actual = try encode(allocator, Value{ .a = null, .b = 5 });
    defer allocator.free(actual);

    try std.testing.expectEqualSlices(u8, &.{ 0x30, 0x03, 0x02, 0x01, 0x05 }, actual);
}

test "encode preserves outer sequence tag after implicit field tags" {
    const Value = struct {
        a: u8,
        b: u8,

        pub const asn1_tags = .{
            .a = asn1.FieldTag.initImplicit(0, .context_specific),
        };
    };
    const allocator = std.testing.allocator;
    const actual = try encode(allocator, Value{ .a = 1, .b = 2 });
    defer allocator.free(actual);

    try std.testing.expectEqualSlices(u8, &.{ 0x30, 0x06, 0x80, 0x01, 0x01, 0x02, 0x01, 0x02 }, actual);
}

test {
    _ = Decoder;
    _ = Encoder;
}