Javascript: Credit card validation

17
Aug
0

Here’s a quick Javascript I wrote to validate (The 4 major American) credit card numbers, based on this post by Harrell W. Stiles.

Validate a credit card number with Javascript.

View the source on that page to grab the code, here are the two main functions as a quick reference:

function get_cc_type(n){
var n2 = n.substr(0,2);
var n4 = n.substr(0,4);
var n1 = n.substr(0,1);
var l = n.length

if(n4 == “6011″ && l == 16){
return “discover”;
} else if(n1 == “4″ && l > 12 && l < 17){
return “visa”;
} else if(n2 == “51″ || n2 == “52″ || n2 == “53″ || n2 == “54″ || n2 == “55″ && l == 16){
return “mastercard”;
} else if(n2 == “34″ || n2 == “37″ && l == 15){
return “american_express”;
} else {
return “unknown”;
}
}

function is_valid_cc_number(n){
var toggle = 0;
var total = 0;
n = n.split(”").reverse();
for(i=0;i<n.length;i++){
if(toggle == 0){
val = n[i];
toggle = 1;
} else {
val = n[i] * 2;
if(val > 9){
tempVal = val.toString().split(”");
val = parseInt(tempVal[0]) + parseInt(tempVal[1]);
}
toggle = 0;
}
total = parseInt(total) + parseInt(val);
}

if(total % 10 === 0){
return true;
} else {
return false;
}
}

A copy of the original post follows.

Credit Card Validation - Check Digits

This document outlines procedures and algorithms for Verifying
the accuracy and validity of credit card numbers. Most credit
card numbers are encoded with a “Check Digit”. A check
digit is a digit added to a number (either at the end or the beginning)
that validates the authenticity of the number. A simple algorithm
is applied to the other digits of the number which yields the
check digit. By running the algorithm, and comparing the check
digit you get from the algorithm with the check digit encoded
with the credit card number, you can verify that you have correctly
read all of the digits and that they make a valid combination.

Possible uses for this information:

  • When a user has keyed in a credit card number (or scanned
    it) and you want to validate it before sending it our for debit
    authorization.
  • When issuing cards, say an affinity card, you might want to
    add a check digit using the MOD 10 method.

1.Prefix, Length, and Check Digit Criteria

Here is a table outlining the major credit cards that you might
want to validate.

CARD TYPE Prefix Length Check digit algorithm
MASTERCARD 51-55 16 mod 10
VISA 4 13, 16 mod 10
AMEX 34

37

15 mod 10
Diners Club/

Carte Blanche

300-305

36

38

14 mod 10
Discover 6011 16 mod 10
enRoute 2014
2149
15 any
JCB 3 16 mod 10
JCB 2131

1800

15 mod 10

2. LUHN Formula (Mod 10) for Validation of Primary Account
Number

The following steps are required to validate the primary account
number:

Step 1: Double the value of alternate digits of the primary
account number beginning with the second digit from the right
(the first right–hand digit is the check digit.)

Step 2: Add the individual digits comprising the products
obtained in Step 1 to each of the unaffected digits in the original
number.

Step 3: The total obtained in Step 2 must be a number ending
in zero (30, 40, 50, etc.) for the account number to be validated.

For example, to validate the primary account number 49927398716:

Step 1:

        4 9 9 2 7 3 9 8 7 1 6

         x2  x2  x2  x2  x2

------------------------------

         18   4   6  16   2

Step 2: 4 +(1+8)+ 9 + (4) + 7 + (6) + 9 +(1+6) + 7 + (2)
+ 6

Step 3: Sum = 70 : Card number is validated

Note: Card is valid because the 70/10 yields no remainder.

The great folks at ICVERIFY
are the original source of this data, I only formatted it in HTML.

If you are in the market, I wrote a set of FoxPro

modules for Windows/Dos that interface nicely with ICVERIFY
in a multi-user LAN setup. You just set up ICVERIFY
on a single station, and all stations on the LAN can authorize
credit cards with a single FOXBASE function call. Of course, you
have to license ICVERIFY by the node, but it is very reasonable.
I also wrote a couple of simple functions to perform pre-authorization,
card screening, etc.

Here is a Microsoft Excel worksheet
that will validate a number for you (useful for understanding
the algorithm, it is in a .ZIP compressed format)

Horace Vallas made a NeoWebScript (Tcl really) procedure that
implements it.

Check it out at https://enterprise.neosoft.com/secureforms/hav/

Because I get at least a letter a week regarding this routine,
here are some additional helpful notes:

Make sure that you:

  1. have started with the rightmost digit (including the check
    digit) (figure odd and even based upon the rightmost digit being
    odd, regardless of the length of the Credit Card.) ALWAYS work
    right to left.
  2. the check digit counts as digit #1 (assuming that the rightmost
    digit is the check digit) and is not doubled
  3. double every second digit (starting with digit # 2 from the
    right)
  4. remember that when you double a number over 4, (6 for example)
    you don’t add the result to your total, but rather the sum of
    the digits of the result (in the above example 6*2=12 so you would
    add 1+2 to your total (not 12).
  5. always include the Visa or M/C/ prefix.

PHPBB3 - Delete spam users and posts

26
May
26

One particular forum that I administrate recently had a problem with an influx of spam users and posts.  We needed a solution but until I could come up with one we needed a quick way to get rid of the trash they were posting on our public forums, so I wrote this script.  

The script accepts a username as input, and it will remove every trace of that user from the PHPBB database.  It deletes their username, bans their username and IP Address, removes any posts and/or topics created by them, and corrects the ‘last post by’ on each forum by removing them.

Feel free to use this script if you like, but be aware it’s very dangerous as it’s directly editing the PHPBB3 database tables.  The only configuration you need to supply is the database name, hostname, and auth credentials.  The script also assumes you used the default table prefix (phpbb_) when installing.

Code: http://ryanbrotherton.pastebin.com/f2dd5c4a3

JavaScript: Image Rotator

26
May
17

One day I found myself in need of an image rotator, but instead of grabbing a pre-made script I decided to write my own.  Why re-invent the wheel you ask?  Because I like to learn and I love JavaScript, I don’t get to write custom JavaScript nearly enough.  Also, to better understand how this particular wheel works.  I want to re-write this eventually  to remove the dependency on Scriptaculous.

I shouldn’t have to say this but if you want to use it, you’re free to use it for any purpose.

Dependencies: Scriptaculous - For the fading effect.

Code: http://ryanbrotherton.pastebin.com/f1cc2203c

Google pushing Chrome to IE users

17
Apr
0

This morning I noticed this in the upper right hand corner of the screen on Google’s home page

It turns out they are only displaying it for users who come to the Google page using Internet Explorer.

IE6

IE7

Firefox

Filed under: Web

Internet Explorer 6 is AWESOME!

11
Feb
3

Standards

Daniel Miessler

The absolute worst browser when it comes to supporting the standards is Internet Explorer.

The Internet works for one simple reason - everything at its core has been built on agreements that bind it together. Whether a computer is connected from California or Sri Lanka, it’s going to speak the same language and obey the same rules - the rules defined by standards. If this weren’t the case there would be no Internet at all.

The designers of Internet Explorer have purposely turned their back on the standards designed to benefit the Internet as a whole. They have done this for years, continue to do it today, and appear to have nothing but their own interests at heart.

http://dmiessler.com/writing/dumpie/

Free the Web

IE6 is the bane of every web developer’s life. Released in 2001, IE6 fails to even properly support the CSS 1.0 standard from 1996.

Internet Explorer 6 is holding back the future.

Supporting IE6 prevents us from using cool new features, standard with up to date browsers. This erodes user-experience for everyone. Additionally, the hacks and workarounds that web developers are forced to use degrades their code, and this limits progress in other areas. Above all it’s simply a waste of millions of hours of human potential.

http://www.free-the-web.com/

Security

Daniel Miessler

What makes other browsers better than IE at protecting vs. spyware and other attacks? Well, it’s simple really - most other browsers don’t make it so easy to install malicious software on your system without you knowing about it. IE makes it relatively trivial through two features called ActiveX and Active Scripting. These technologies were designed specifically for the purpose of giving Web sites more control over a user’s computer. Unfortunately, as we have seen with exploit after exploit - that’s not always a good thing.

http://dmiessler.com/writing/dumpie/

Bruce Schneier - Security Expert

This study is from August, but I missed it. The researchers tracked three browsers (MSIE, Firefox, Opera) in 2004 and counted which days they were “known unsafe.” Their definition of “known unsafe”: a remotely exploitable security vulnerability had been publicly announced and no patch was yet available.

MSIE was 98% unsafe. There were only 7 days in 2004 without an unpatched publicly disclosed security hole.

Firefox was 15% unsafe. There were 56 days with an unpatched publicly disclosed security hole. 30 of those days were a Mac hole that only affected Mac users. Windows Firefox was 7% unsafe.

http://www.schneier.com/blog/archives/2005/12/internet_explor.html

PC World

In 2006, citing its lack of security, PC World magazine named Internet Explorer 6 number 8 on their list of the “25 worst tech products of all time”.

http://www.pcworld.com/article/125772-3/the_25_worst_tech_products_of_all_time.html

Current

Just recently, A major flaw in Microsoft’s Internet Explorer that allows hackers to gain the password details of the user was revealed.

This is not a rumor, it was confirmed by Microsoft who in fact announced the discovery themselves admitting a “vulnerability in Internet Explorer” that “could allow remote code execution.” Not Good.

http://thenextweb.com/2008/12/16/dump-internet-explorer-at-least-for-now/

Market Share

These stats are accurate to anyones guess, bost most claim google as a source.  This is an averaging of the best sources I could find.  (w3c puts firefox at 44%, I didn’t use them because that seemed way out of line with all other sources)

  • IE7 - 47.32%
  • Firefox: 21%
  • IE6 - 19.21%
  • Safari - 8%
  • Chrome/Opera - 2%

Conclusion

It is becoming more and more clear that, as some of our quoted authors have suggested, IE6 is severely impeding the progress of the web as a whole.  You could say this of any inferior or outdated browser but IE6 is the only one still widely used, and the cause of its continued widespread use is uneducated users.  They remain uneducated because they are unaware of the inherent security risks in IE6 due to a tight integration with the Windows operating system and exploitable access to ActiveX controls.

IE6 is not only an incredible time sink and headache for developers, it is a danger to consumers as well.

Spreadfirefox Affiliate Button

imgsize.net opens as beta.

20
Jan
0

http://www.imgsize.net

I got bored last weekend and started writing this.  I opened the doors as beta so people can easily use it to resize simple images if they’re in a pinch or if they don’t have an image manipulation tool readily available.

It’s also great for people who aren’t really tech savvy.

The primary goals for the project were ease of use, simplicity, and an intuitive interface with as few steps as possible to get a quickly resized image.

Planned Features/Fixes

  • Manual Input of dimensions (w/ constrained proportions)
  • Allow slider to increment 1% instead of only 2%
  • Transparency Support?
  • Web Service of some sort?
Using the service is as easy as uploading an image, adjusting a slider to change the size, and clicking save:

Windows 7 Review

3
Jan
755

Quick Summary:

  • I obtained the Windows 7 beta (build 7000)
  • I installed it on my desktop
  • I used it for one week, for everything from web browsing to programming and development work
  • It is awesome

Long Review:

Where to begin…I’m going to try and keep this from sounding fanboy-ish.  People who know me know I am no proponent of Microsoft goods anyways, I’m just really impressed with this OS.

Disclaimer: I hate vista, I haven’t used it much so if I’m raving about a feature that was already in vista, forgive me.  I hate it for a multitude of reasons, but we won’t discuss those here.

My desktop specs are as follows:

  • AMD Athlon 64 X2 Dual Core 6000+ 3.01 GHz
  • Started out at 2 gigs of DDR2 800 ram, ended up with 6 gigs.
  • Geforce 8600GTS video - Two 22″ widescreen monitors
  • 250gb hdd

I’m writing this review in the format of a rough outline, just highlighting features I’m really impressed with, so here goes.

Windows 7 Installation

Speed - Holy crap, this thing formatted my drive and windows was installed in probably 20 minutes.  After installation I had to install NO DRIVERS (not even video), it already had them all or found them all without asking me anything.

Ease - The interface for formatting/installing is beautiful and simple.  Very minimalistic and easy to use, it’s a snap to set up.

It took me less than 1 hour to format, install windows, and reinstall all of my necessary software.  Including Firefox, MS Office, Pidgin, Winamp, Code Editors and FTP clients, and many more.

Windows 7 Speed

Prior to installing Win7 on my desktop at home, I installed it on a spare nearby.  An old Dell with a 1.8ghz intel cpu and 512mb of ram.  Even with these limited specs Win7 ran incredibly smooth without a single performance hitch anywhere.

It was no different on my desktop, perfectly smooth everywhere.

It feels so lightweight and fast, the complete opposite of Vista’s clunky feel.  Hopefully this doesn’t change too much between now and the retail release build.

Heres a quick performance comparison of Win7 vs Vista vs XP: http://blogs.zdnet.com/hardware/?p=3236&page=2

Windows 7 Taskbar

I’m not a huge fan of the new icon layout, but with a minor tweak to the taskbar preferences it is perfect.  Just enable the “Use small icons” option and set the “Taskbar Buttons” option to “Combine when taskbar is full” instead of “always combine”.  this will not only make the taskbar vertically smaller but will display windows like WinXP/Vista did.

Apps that are “pinned” to the taskbar are very cool, as the “pinned” icon expands to create the window when you open them.

The new system tray is excellent at conserving space, extra system tray icons are hidden and appear in a pop up when you click the expand arrow.  This is easily configurable, and easy to revert to xp/vista style if you prefer.

Maximized apps overlap the taskbar now, that’s going to take some getting used to.

Windows 7 Search

The Search/Run bar on the start menu now fills the entire start menu with search results, instead of just the left side, making it much easier to read.

The search function in windows explorer is amazing, and now searches as you type.  No more stupid animated figure you have to wait on or disable.  No more seraching, then having to click something to search again.  Just type.

Windows 7 Codecs

Man was I surprised when I went to download VLC media player.  While it was downloading I fired up a video just to see what it would look like and to my amazement it looked and sounded perfect.  Windows 7 comes with codecs already installed, it appears to be the DivX codec pack.

Windows 7 Style

This OS, thanks to the Aero interface, is beautiful.  I’ll have some screen shots up soon.  Everything is amazingly simple to change or modify, and the default themes even support easy customization.

Windows 7 Functionality

The rightmost 10 pixels or so of the taskbar form a small raised button that when clicked, toggles the show desktop function of windows.  This is coming in VERY handy.  It will also allow you to “preview” the desktop if you hover over it with your mouse.  The “preview” function turns every window you have open completely transparent except for that window’s outline.

A type of “locator” feature i installed in the taskbar.  If you mouse over a window on the taskbar, and then mouse over the live preview generated by that, it will turn every window open to transparent, except for the one you are hovering over.

The start menu has some new additions as well, like programs have an expandable menu on hover, that displays recently opened documents with the program.

Windows 7 Programs

It comes with a piece of software called the “Snipping tools” making the process of taking a screen shot much, much easier.  I’ve been told this is similar if not nearly identical to a piece of software that comes with Mac OS.

Paint’s interface got a face lift, and now resembles the interface used by Microsoft Office.

Calculator got a neat little interface tweak.

Resource Monitor - Win7 has an incredible resource monitor for everything from cpu/ram/disk/network monitoring down to the latency of specific processes.

Windows 7 Security and UAC

I disabled UAC as soon as I booted, but overall Win 7 security seems to be much less obtrusive and annoying.  It’s still ridiculous that it asks you if you want to execute something you’d obviously tried to run.

Windows 7 Stability

The OS has an uptime of over a week thus far, it appears to be a very stable beta build.

I’ve only had a couple of programs that would not run correctly.  This issue should be resolved when Win7 is more mainstream and people start writing programs with Win7 support/driver support.

Conclusion

In conclusion, it seems that the primary goal of the Windows 7 development team was to take a crack at the “It just works” mantra that Mac users always seem to brag about, and in my opinion they’ve succeeded.  I can’t wait for a retail build, as this OS is one that will be worth the money.

I’m predicting that Windows 7 marketshare is going to vastly overshadow Vista, and hopefully replace it completely.  Windows 7 is what Vista should’ve been.

Filed under: Reviews

10 digit decimal IP addresses - How to

24
Dec
0

There is a little known quirk in the Internet Explorer and Firefox browsers.  Here’s an example link to Google: http://1249710995.  If clicking it doesn’t work for you, copy and paste it into IE6 or 7 or FF3. (I haven’t tested any other browsers except for Opera and Chrome, neither of them work.)

I wrote a quick converter in PHP, you can play with it here and download the source code on the same page.

How to: Convert each octet of an IPv4 address to hex and concatenate the hex values into one giant number. Convert the number back to decimal and you have the 10 digit number.

Quick reference conversion functions:

IP Address to 10 digit:

function toTen($i){
$a = explode(”.”,$i);
$g1 = $a[0];
$g2 = $a[1];
$g3 = $a[2];
$g4 = $a[3];

$g1 *= 16777216;
$g2 *= 65536;
$g3 *= 256;

return $g1 + $g2 + $g3 + $g4;
}

10 digit back to IP:

function toIP($i){
$i = dechex($i+0);
$g1 = hexdec(substr($i,0,2));
$g2 = hexdec(substr($i,2,2));
$g3 = hexdec(substr($i,4,2));
$g4 = hexdec(substr($i,6,2));

return $g1.”.”.$g2.”.”.$g3.”.”.$g4;
}

Why: Anonymity.  I can’t think of a use for this that isn’t potentially malicious.  It could be used by malware to trick users into going to an unfriendly url, and bypass blacklisted domain names in spam filters.

I’m sure the feature is put there for a reason, I’m just not sure what it is.  Got any ideas?