Skip to content
/ hetu Public

Finnish personal identity code validator and generator in Rust

License

Notifications You must be signed in to change notification settings

jelovirt/hetu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Personal identity code validator and generator in Rust

Crates.io Tests

Simple crate for validating and generating Finnish Personal Identity Code (henkilötunnus, hetu).

Supports the 1.1.2023 format.

Works with rust 1.8 stable.

Usage

Add this to your Cargo.toml

[dependencies.hetu]
git = "https://github.com/jelovirt/hetu.git"

To validate a personal identity code:

extern crate hetu;
use hetu::Ssn;

pub fn main() {
    if Ssn::try_from("121212-121D").is_ok() {
        println!("Valid")
    } else {
        println!("Invalid")
    }
}

To generate a personal identity code:

extern crate hetu;
use hetu::Ssn;

pub fn main() {
    println!("{}", Ssn::generate());
}

To generate a personal identity code by pattern:

extern crate hetu;
use hetu::Ssn;
use hetu::SsnPattern;

pub fn main() {
    let pattern = SsnPattern::try_from("111111-111?").unwrap();
    println!("{}", Ssn::generate_by_pattern(pattern).unwrap());
}

CLI

Command line tool hetu can be used to either validate or randomly generate personal identity codes.

To validate a personal identity code:

$ hetu 121212-121D
$ echo 121212-121D | hetu -
$ hetu 121212-121C
Error: Invalid checksum: expected D
  
  121212-121C
            ^

To generate a personal identity code:

$ hetu
121212-121D

To generate a personal identity code by pattern that can contain wildcards:

$ hetu -p "121212-121?"
121212-121D
$ hetu -p "121212-???D"
121212-028D
$ hetu -p "??????-???D"
241151-028D

Related projects

About

Finnish personal identity code validator and generator in Rust

Topics

Resources

License

Stars

Watchers

Forks

Languages