Giter Club home page Giter Club logo

nor-id-num's Introduction

nor-id-num

Norwegian Identity Numbers

This project is for validating and generating Norwegian identity numbers. There are 3 kinds supported:

  • Organization numbers
  • Birth numbers
  • D-numbers

Organization numbers

Organization numbers identify commercial organizations as well as non-profit organizations. The identifying numbers consist of 9 digits. The first digit is either 8 or 9. The last digit is a checksum.

Birth numbers

Birth numbers identify individual persons. Every Norwegian citizen is given a unique birth number within a few days after being born. Foreigners may acquire a birth number e.g. when granted citizenship. The numbers consist of 11 digits. The first 6 digits are the birth date (DDMMYY), the next 3 digits constitute an individual number for the particular date, and the last 2 digits are both checksum digits. Ranges within the individual number determine the century. The last digit of the individual number determines gender (even numbers for female individuals and odd numbers for male).

D-numbers

D-numbers is used to identify foreign individuals that don’t qualify for a birth number. The D-number is constructed exactly the same way as birth numbers, but the value 4 is added to the very first digit (e.g. the date 311299 turns into 711299).

Visual Studio solution

The Visual Studio solution consists of 4 projects:

  • NinEngine
  • NinCmd
  • NinUi
  • UnitTest

NinEngine

This is the core project providing functionality for validating and generating identity numbers. This is all you need if you want to consume the validation or generation functionality in your own code.

NinCmd

This is just a sample command line interface to play with the capabilities of the core module.

NinUi

This is just a sample window user interface to play with the capabilities of the core module.

UnitTest

You guessed it – a project implementing unit tests for the core engine module.

Using the code

There are three main classes: OrganizationNumber, BirthNumber and DNumber.

Instantiating and validating

The classes all have a constructor accepting a string. If the string doesn’t represent a valid identification number, an exception (a NinException object) is thrown. This object holds detailed information about what is wrong with the number. Also, there is a static Create method, returning null if the passed number doesn’t represent a valid identification number. This way you don’t get any information about the reason for rejecting the number passed.

Generating one number at a time

Completely random

The static method OneRandom generates a random (valid) identification number. This will always succeed.

Matching a pattern

Another incarnation of OneRandom accepts a pattern and optionally a retry count. Using this method you can force particular digits in particular positions and let other positions have a random digit. For each position, provide either a digit where you want to force or a question mark where you want to randomize. This may or may not produce a valid identification number. For instance, if you force the digits 99 in the two positions representing month in a birth number pattern, it is not possible to generate a valid number. The method will give up after a number of failed retries, returning null.

Date boundaries and gender

For birth numbers and D-numbers, there is yet another variation of the OneRandom method, accepting two dates and a gender request. The generated number will always represent a birth date between (inclusive) the two dates. Also, if you request a particular gender, the generated number will match that.

Generating several numbers at once

All possible

The static method AllPossible returns all possible numbers in an IEnumerable<> collection. The number of legal possibilities is found in the constant PossibleLegalVariations (18,181,818 for organization numbers, 26,412,179 for birth numbers, and 26,412,204 for D-numbers). Warning: Generating all those numbers may consume a considerable amount of time! Be patient, the method will eventually finish.

As many as you want – slow

The static method ManyRandom accepts a count and returns that many identity numbers in an IEnumberable<> collection. First all possible numbers are generated, then the requested number are picked from those generated. This way, generating 10 numbers takes (almost) as long as generating 10,000,000. Using this slow approach guarantees that you will get the requested count of numbers, with no duplicates.

As many as you want – fast

The static method QuickManyRandom accepts, like ManyRandom, a count, and returns that many identity numbers in an IEnumerable<> collection. To accomplish this, OneRandom is called a number of times. Using this speedier approach may produce duplicates in the result list.

nor-id-num's People

Contributors

hkhaug avatar jcdekoning avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

nor-id-num's Issues

OneRandom with GenderRequest

It would be nice to have an overload for generating a fødselsnumber with only gender request, as this is a scenario I often face in my test :)

Invalid fødselsnummer when generating with a date

When generating a new fødselsenummer using fromDate = toDate = 21.03.1975, the generator once returned 21037551446. This is a valid fødselsnummer, but implies a birth year where the century is even (i.e., 1875) which is invalid given the birth year 1975.

Edit: Removed 2075 as valid year.

< 1900

Stemmer det at det ikke er støtte for fødselsnummer for før 1900? F.eks 01018487876 - 01.01.1884

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.