Last active
August 29, 2015 14:05
-
-
Save turhn/c9ad8e73780b1be5f130 to your computer and use it in GitHub Desktop.
TCKN Verifier - Turkish Citizenship Number Checker - TCKN Kimlik No Doğrulama
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### | |
# Author : Turhan Coskun | |
# Name : TCKN Number Verification - TCKN Dogrulama | |
# Usage : ruby tckn_verifier.rb <turkish citizenship number> | |
### | |
raw_id_number = ARGV[0] # Assign the first argument of the program to the variable | |
# Extensions of the String class | |
class String | |
def numeric? # Check if the given string is numeric | |
Float(self) != nil rescue false | |
end | |
def to_a # Convert the string into a char array | |
arr = [] | |
self.each_char do |c| arr << c end | |
return arr | |
end | |
end | |
def tckn_verifier(tckn) | |
tckn_id_number = tckn.to_s # Function arg converted to a string for manupulation | |
if tckn_id_number.length != 11 # TCKN must be 11 digits | |
return false | |
end | |
if not tckn_id_number.numeric? # TCKN must be numerical | |
return false | |
end | |
if tckn_id_number[0] == 0 # TCKN cannot start with "0" | |
return false | |
end | |
digits = tckn_id_number.to_a.collect do |n| n.to_i end # String array converted to an integer array | |
# Implementation of the TCKN algorythm | |
sum = 0 | |
digits[0..9].each do |i| | |
sum += i # Get sum of the first 10 digits | |
end | |
if sum % 10 != digits[10] | |
return false # Modulus 10 of the first 10 digits must point to the last digit | |
end | |
sum_odd=0 | |
sum_even=0 | |
order=0 | |
digits[0..9].each do |i| | |
order += 1 | |
if order.even? | |
sum_even += i | |
else | |
sum_odd += i | |
end | |
end | |
if(sum_odd*7 - sum_even) % 10 == digits[9] | |
return true | |
end | |
return true # If we got so far, we did all good | |
end | |
if tckn_verifier(raw_id_number) | |
puts("This is a VALID TCKN") | |
else | |
puts("INVALID TCKN") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment