Zig
Zig
Password_Digest = Base64 (SHA-1 ( nonce + created + SHA-1 (password) ) )
See more Encryption Examples
Demonstrates how to compute:Password_Digest = Base64 (SHA-1 ( nonce + created + SHA-1 (password)))
Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const password = "secret";
const crypt = try chilkat.Crypt2.init();
defer crypt.deinit();
crypt.setHashAlgorithm("SHA-1");
crypt.setEncodingMode("base64");
// Generate a 16-byte random nonce
const prng = try chilkat.Prng.init();
defer prng.deinit();
const bd = try chilkat.BinData.init();
defer bd.deinit();
prng.genRandomBd(16, bd) catch {};
// Get the current date/time in a string with this format: 2010-06-08T07:26:50Z
const dt = try chilkat.DateTime.init();
defer dt.deinit();
dt.setFromCurrentSystemTime() catch {};
const created = try dt.getAsTimestamp(alloc, false);
bd.appendString(created, "utf-8") catch {};
// This example wishes to calculate a password digest like this:
// Password_Digest = Base64 ( SHA-1 ( nonce + created + SHA-1(password) ) )
// First SHA-1 digest the password...
const password_sha1 = try crypt.hashStringENC(alloc, password);
// Append the 20 binary bytes of the SHA1 hash to bd, which already contains the nonce and created date/time.
bd.appendEncoded(password_sha1, "base64") catch {};
const password_digest = try crypt.hashBdENC(alloc, bd);
std.debug.print("Base64 password digest = {s}\n", .{password_digest});
}