blog.sojoodi.com

September 14, 2007

Luhn algorithm for credit card validation

Filed under: Ruby — Sahand @ 3:58 pm

I came across an interesting article on algorithms in, of all places, The Economist. It briefly describes the Luhn algorithm for credit card validation. So I hacked together the following piece of Ruby code which does just that.

print "Enter card number: "
cc_number = gets.chomp.tr(' -','')
checksum = 0
double = false
cc_number.reverse.each_byte do |digit|
  dig = digit.chr.to_i
  checksum += (double ? (dig*2)%9 : dig)
  double = !double
end

if checksum%10 == 0
  puts "valid CC number"
else
  puts "Invalid"
end

For more interesting articles on this, see the following:

http://www.darkcoding.net/index.php/credit-card-numbers/
http://www.merriampark.com/anatomycc.htm

September 5, 2007

Welcome!

Filed under: Blogroll — Sahand @ 9:28 pm

Hello,

Thank you for visiting my blog. I am a Computer Engineer by training who has recently turned from hardware programming and modeling to web-application development. I am currently a co-founder of an online service site which you will hear about soon I hope.

This blog will be mostly technical for now, containing information about my week-to-week learnings in this new field — Ruby on Rails to be exact. However, I’ll try to include other topics as well if I come across interesting ones.

I hope you come across useful information in the blog and enjoy it.

Sahand

« Previous Page

© 2007 Sahand Sojoodi
Powered by WordPress