aboutsummaryrefslogtreecommitdiff
path: root/src/storage/session-manager/session.zig
blob: 943334bf8c843c088fb01f33d1cba1dc8acf661e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const std = @import("std");
const User = @import("../user.zig");

const Self = @This();

const fingerprint_size = 512;

age: std.time.Instant,
info: User.Info,
fingerprint: []u8,

pub fn init(allocator: std.mem.Allocator, info: User.Info) !Self {
    var self: Self = undefined;
    self.info = try info.clone(allocator);
    self.fingerprint = try allocator.alloc(u8, fingerprint_size);
    try self.reset();

    return self;
}

pub fn reset(self: *Self) !void {
    var raw_buffer: [fingerprint_size / 2]u8 = undefined;
    std.crypto.random.bytes(&raw_buffer);

    var writer = std.Io.Writer.fixed(self.fingerprint);
    writer.print("{x}", .{raw_buffer}) catch unreachable;

    self.age = try std.time.Instant.now();
}

pub fn deinit(self: *const Self, allocator: std.mem.Allocator) void {
    self.info.deinit(allocator);
    allocator.free(self.fingerprint);
}