1. 程式人生 > >Luhn algorithm(附信用卡校驗演算法C語言實現)

Luhn algorithm(附信用卡校驗演算法C語言實現)

From Wikipedia, the free encyclopedia

The Luhn algorithm or Luhn formula, also known as the "modulus 10" or "mod 10" algorithm, is a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers, IMEI numbers, National Provider Identifier numbers in US and Canadian Social Insurance Numbers. It was created by IBM scientist Hans Peter Luhn and described in U.S. Patent No. 2,950,048, filed on January 6, 1954, and granted on August 23, 1960.

The algorithm is in the public domain and is in wide use today. It is specified in ISO/IEC 7812-1[1]

. It is not intended to be a cryptographically secure hash function; it was designed to protect against accidental errors, not malicious attacks. Most credit cards and many government identification numbers use the algorithm as a simple method of distinguishing valid numbers from collections of random digits.

Contents

  • 1 Strengths and weaknesses
  • 2 Informal explanation
  • 3 Mod 10+5 Variant
  • 4 Implementation of standard Mod 10
  • 5 Other implementations
  • 6 References

Strengths and weaknesses

The Luhn algorithm will detect any single-digit error, as well as almost all transpositions of adjacent digits. It will not, however, detect transposition of the two-digit sequence 09

to 90 (or vice versa). It will detect 7 of the 10 possible twin errors (it will not detect 2255, 3366 or 4477).

Other, more complex check-digit algorithms (such as the Verhoeff algorithm) can detect more transcription errors. The Luhn mod N algorithm is an extension that supports non-numerical strings.

Because the algorithm operates on the digits in a right-to-left manner and zero digits affect the result only if they cause shift in position, zero-padding the beginning of a string of numbers does not affect the calculation. Therefore, systems that normalize to a specific number of digits by converting 1234 to 00001234 (for instance) can perform Luhn validation before or after the normalization and achieve the same result.

The algorithm appeared in a US Patent for a hand-held, mechanical device for computing the checksum. It was therefore required to be rather simple. The device took the mod 10 sum by mechanical means. The substitution digits, that is, the results of the double and reduce procedure, were not produced mechanically. Rather, the digits were marked in their permuted order on the body of the machine.

Informal explanation

The formula verifies a number against its included check digit, which is usually appended to a partial account number to generate the full account number. This account number must pass the following test:

  1. Counting from the check digit, which is the rightmost, and moving left, double the value of every second digit.
  2. Sum the digits of the products together with the undoubled digits from the original number.
  3. If the total ends in 0 (put another way, if the total modulo 10 is equal to 0), then the number is valid according to the Luhn formula; else it is not valid.

As an illustration, if the account number is 49927398716, it will be validated as follows:

  1. Double every second digit, from the rightmost: (1×2) = 2, (8×2) = 16, (3×2) = 6, (2×2) = 4, (9×2) = 18
  2. Sum all the individual digits (digits in parentheses are the products from Step 1): 6 + (2) + 7 + (1+6) + 9 + (6) + 7 + (4) + 9 + (1+8) + 4 = 70
  3. Take the sum modulo 10: 70 mod 10 = 0; the account number is valid.

Mod 10+5 Variant

Some credit cards use the "Mod 10 plus 5" variant to extend the space of valid card numbers.[citation needed] In this variant, if the sum ends in 0 or 5, the number is considered valid.

Implementation of standard Mod 10

Python variant:

def is_mod10(cc):
    dub, tot = 0, 0
    for i in range(len(cc) - 1, -1, -1):
        for c in str((dub + 1) * int(cc[i])):
            tot += int(c)
        dub = (dub + 1) % 2
    return (tot % 10) == 0

Java variant:

  public static boolean isValidCC(String num) {
 
    final int[][] sumTable = {{0,1,2,3,4,5,6,7,8,9},{0,2,4,6,8,1,3,5,7,9}};
    int sum = 0, flip = 0;
 
    for (int i = num.length() - 1; i >= 0; i--, flip++)
      sum += sumTable[flip & 0x1][num.charAt(i) - '0'];
    return sum % 10 == 0;
  }

c variant:

這裡有一個表格,概述了主要的信用卡,您可能要驗證。

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 51-55

16 16

10 mod 10

簽證 VISA

4 4

1316 13, 16

10 mod 10

美國證券交易所 AMEX

34 34
37 37

15 15

10 mod 10

大來卡/ Diners Club/

全權委託 Carte Blanche

300-305 300-305
36 36
38 38

14 14

10 mod 10

探索 Discover

6011 6011

16 16

10 mod 10

途中 enRoute

2014 2014
2149 2149

15 15

任何 any

聯合協調 JCB

3 3

16 16

10 mod 10

聯合協調 JCB

2131 2131
1800 1800

15 15

10 mod 10

相關推薦

Luhn algorithm信用卡演算法C語言實現

From Wikipedia, the free encyclopedia The Luhn algorithm or Luhn formula, also known as the "modulus 10" or "mod 10" algorithm, is a simple checksum formu

SHA1演算法C語言實現

SHA1 安全雜湊演算法:對於長度小於2^64位的訊息(1M = 1024k,1K = 1024位元組,1BYTE = 8bit 可以想象一下2的63次方位可以表示一個多大的資料檔案),SHA1會產生一個160位的訊息摘要。當接收到訊息的時候,這個訊息摘要可以用來驗證資料的

CRC32演算法C語言版(查表法)

最近用到CRC校驗演算法,就找了些資料,學習了一下,網上關於CRC32的資料也多,但感覺不是很完整,或者太高深。 CRC演算法查表法很常見,但表是怎麼來的,有些資料說得不很清楚。 我來說一下我的看法:

CRCC語言實現

ins rcc param into phoenix 兩個 The ide align 文章轉自 循環冗余校驗(CRC)算法入門引導 - Ivan 的專欄 - 博客頻道 - CSDN.NET http://blog.csdn.net/liyuanbhu/article/de

crc32c語言實現

最近在做軟體升級,需要對升級檔案進行crc校驗,就學習了crc的實現原理 crc就是一個數值,該數值用於檢驗資料的正確性,crc校驗的原理就是將需要作校驗的資料與一個數據模2相除,得到的餘數即為校驗值。       模2相除就是在除的過程中用模2加,模2加實際上就是異或

CRC16C語言實現

一、目的 闡述CRC16的原理,並以C語言程式碼實現。二、 校驗碼的作用 校驗碼用於校驗資料的有效性/正確性。 校驗碼用原資料生成,並伴隨原資料一起傳送/儲存,使用者拿到傳送/儲存的資料序列後,取出原資料部分,根據校驗碼生成規則生成校驗碼,與拿到的校驗碼進行比較即可判斷資料是

連結串列和順序表參考stl原始碼,使用c語言實現

順序表 優點:可以隨機訪問,cpu快取記憶體利用率高,不涉及(較少)進行插入和刪除操作,應該使用順序表。 順序表,不論是動態開闢,還是固定大小,一般放置在棧上,不用程式設計師手動開闢空間 連結串列:主要運用2種,A單向不帶頭結點的非迴圈連結串列      B雙向帶頭

NandFlash ECC 演算法原理與實現

ECC的全稱是Error Checking and Correction,是一種用於Nand的差錯檢測和修正演算法。如果操作時序和電路穩定性不存在問題的話,NAND Flash出錯的時候一般不會造成整個Block或是Page不能讀取或是全部出錯,而是整個Page(例如5

位反轉的最佳演算法C語言實現

green_t 提問: 實現如下轉換的最佳演算法是什麼? 0010 0000 => 0000 0100 具體的轉換是從MSB->LSB 到 LSB->MSB,所有的位都必須反轉,那意味著,這並不是位元組順序的交換。 LSB(Least Signi

有趣的演算法:3分鐘看懂希爾排序C語言實現

在上一次的演算法討論中,我們一起學習了直接插入排序。它的原理就是把前i個長度的序列變成有序序列,然後迴圈迭代,直至整個序列都變為有序的。但是說來說去它還是一個時間複雜度為(n^2)的演算法,難道就不能再進一步把時間複雜度降低一階麼?確實,以上幾種演算法相對於之前的O(n^2)

C語言實現磁碟排程——掃描尋道演算法SCAN

一、設計目的:       加深對請求磁碟排程管理實現原理的理解,掌握磁碟排程演算法中的掃描尋道演算法。二、設計內容通過程式設計實現磁碟排程中掃描尋道演算法。設定開始磁軌號尋道範圍,依據起始掃描磁軌號和最大磁軌號數,隨機產生要進行尋道的磁軌號序列。選擇磁碟排程演算法,顯示該演

C語言實現磁碟排程——最短尋道優先演算法SSTF

一、設計目的:     加深對請求磁碟排程管理實現原理的理解,掌握磁碟排程演算法中的最短尋道優先演算法。二、設計內容通過程式設計實現磁碟排程中最短尋道優先演算法。設定開始磁軌號尋道範圍,依據起始掃描磁軌號和最大磁軌號數,隨機產生要進行尋道的磁軌號序列。選擇磁碟排程演算法,顯示

處理機排程演算法C語言實現註釋得當!!

/* created by herbert on 10 Nov */ #include <iostream> #include <queue> #include <algorithm> #include <c

資料結構排序演算法之歸併排序c語言實現

博主身為大二萌新,第一次學習資料結構,自學到排序的時候,對於書上各種各樣的排序演算法頓覺眼花繚亂,便花了很長的時間盡力把每一個演算法都看懂,但限於水平有限,可能還是理解較淺,於是便將它們逐個地整理實現出來,以便加深理解。 歸併排序就是通過將一個具有n個key記錄的線性表,看

幾種常用的排序演算法c語言實現

概述 最近重新回顧了一下資料結構和演算法的一些基本知識,對幾種排序演算法有了更多的理解,也趁此機會通過部落格做一個總結。 1.選擇排序-簡單選擇排序 選擇排序是最簡單的一種基於O(n2)時間複雜度的排序演算法,基本思想是從i=0位置開始到i=n-1

m選n組合的兩種演算法C語言實現

原問題:  Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. 1. 遞迴演算法 即首先選擇n,然後遞迴地從剩下的1...n-1選擇k-1

排序演算法-合併排序C語言實現

都說“演算法是程式的靈魂”,而排序是計算機儲存控制方面不能沒有的操作。它在資料的存取,查詢搜尋,資料統計這些基礎資料操作方面有著重要的應用。所以排序演算法是必須是很有研究的。 這次,我學習的是-歸併排序演算法。據說該演算法是馮諾依曼發明的。這個排序演算法比起直

計數排序演算法C語言實現

計數排序,  比較適合數值跨度比較小的,  也就是陣列中最大值減去最小值得到的值儘量小,  同時陣列元素又比較多的情況下用計數排序效率比較高,同時,計數排序演算法基友穩定性。 計數排序的時間複雜度為O(n),計數排序是用來排序0到100之間的數字的最好的演算法。 演算法的步

MD5演算法 —— C語言實現字串的加密

網上找到的實現md5函式程式碼,包括一個頭檔案md5.h和一個原始檔md5.c,用下面的測試程式碼test.c測試通過,各檔案依次如下: .h檔案——md5.h #ifndef MD5_H #define MD5_H typedef struct { unsi