Skip to content

Instantly share code, notes, and snippets.

@adilkhash
Last active January 3, 2025 08:34
Show Gist options
  • Save adilkhash/20f4cf3202ca084b5d0040497be38660 to your computer and use it in GitHub Desktop.
Save adilkhash/20f4cf3202ca084b5d0040497be38660 to your computer and use it in GitHub Desktop.
Validation of IIN of Kazakhstan
const std = @import("std");
const process = std.process;
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
const args = try std.process.argsAlloc(allocator);
defer std.process.argsFree(allocator, args);
if (args.len != 2) {
const stderr = std.io.getStdErr().writer();
try stderr.print("Usage: {s} <iin>\n", .{args[0]});
std.process.exit(1);
}
const iin = args[1];
const stdout = std.io.getStdOut().writer();
var buf: [12]i8 = undefined;
for (0.., iin) |i, char| {
buf[i] = @intCast(char - '0');
}
const w1 = [11]i8{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
const w2 = [11]i8{3, 4, 5, 6, 7, 8, 9, 10, 11, 1, 2};
var checksum: i16 = multiply(buf, w1);
if (checksum == 10) {
checksum = multiply(buf, w2);
}
if (checksum != buf[11]) {
try stdout.print("Invalid IIN\n", .{});
std.process.exit(1);
}
try stdout.print("Valid IIN\n", .{});
}
pub fn multiply(iin: [12]i8, w: [11]i8) i16 {
var result: [11]i8 = undefined;
var checksum: i16 = 0;
for (0..11) |i| {
result[i] = iin[i] * w[i];
checksum += result[i];
}
return @mod(checksum, 11);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment