开发者

Can you help me understand this C# function?

I would like to write the following C# function in ColdFusion, but I am unable to because I can't understand the code. I know the function checks the validity of an 11 digit CP开发者_C百科F number (Brazilian equivalent of US SSN) using a mod operation. I have absolutely no experience with C#.

The full function can be read in this article.

I don't understand where cpf[0], cpf[1], etc... come from, and what the number between the square brackets refers to.

//compute 1st verification digit.
var v1 = 10 * cpf[0] + 9 * cpf[1] + 8 * cpf[2] + 7 * cpf[3] + 6 * 
    cpf[4] + 5 * cpf[5] + 4 * cpf[6] + 3 * cpf[7] + 2 * cpf[8];
v1 = 11 - v1 % 11;
if (v1 >= 10)
    v1 = 0;
//compute 2nd verification digit.
var v2 = 11 * cpf[0] + 10 * cpf[1] + 9 * cpf[2] + 8 * cpf[3] + 7 * 
    cpf[4] + 6 * cpf[5] + 5 * cpf[6] + 4 * cpf[7] + 3 * cpf[8];
v2 += 2 * v1;
v2 = 11 - v2 % 11;
if (v2 >= 10)
    v2 = 0;
//True if verification digits are as expected.
return v1 == cpf[9] && v2 == cpf[10];


Given the following CPF 012.345.678-90

//compute 1st verification digit.
var v1 = 10 * cpf[0] + // 10 x 0 = 0
    9 * cpf[1] +       // 9  x 1 = 9
    8 * cpf[2] +       // 8  x 2 = 16
    7 * cpf[3] +       // 7  x 3 = 21
    6 * cpf[4] +       // 6  x 4 = 24
    5 * cpf[5] +       // 5  x 5 = 25
    4 * cpf[6] +       // 4  x 6 = 24
    3 * cpf[7] +       // 3  x 7 = 21
    2 * cpf[8]         // 2  x 8 = 16
; // result = 156

v1 = 11 - v1 % 11;     // 11 - 156 % 11 = 11 - 2 = 9
if (v1 >= 10)
    v1 = 0;            // 9 >= 10 ? v1 = 9

//compute 2nd verification digit.
var v2 = 11 * cpf[0] + // 11 x 0 = 0
    10 * cpf[1] +      // 10 x 1 = 10
    9 * cpf[2] +       // 9  x 2 = 18
    8 * cpf[3] +       // 8  x 3 = 24
    7 * cpf[4] +       // 7  x 4 = 28
    6 * cpf[5] +       // 6  x 5 = 30
    5 * cpf[6] +       // 5  x 6 = 30
    4 * cpf[7] +       // 4  x 7 = 28
    3 * cpf[8]         // 3  x 8 = 24
;

v2 += 2 * v1;          // 192 + 2 * 9 = 192 + 18 = 210
v2 = 11 - v2 % 11;     // 11 - 210 % 11 = 11 - 1 = 10
if (v2 >= 10)
    v2 = 0;            // 10 >= 10 ? v2 = 0

//True if verification digits are as expected.
// v1 == 9 && v2 == 0
return v1 == cpf[9] && v2 == cpf[10];

It just breaks to pure math


cpf[0] to cpf[10] are presumably the 11 digits of the CPF number. Presumably, this consists of 9 digits of "real" data, and 2 digits of checksum data. Presumably, these 2 digits are calculated as a linear combination of the other 9 digits, in modulo-11 arithmetic.

So this function is recomputing the checksum (v1 and v2), and then checking it matches the received checksum digits (cpf[9] and cpf[10]).

These checksum relationships are typically designed to prevent mistyping digits, or accidentally swapping neighbouring digits (similar schemes are used for ISBN, for example).


It's just the application of the number validation algorithm. cpf is 11-digit number you are checking.

There is nothing complex and C#-specific in the code.


The article to which you linked appears to only show partial c# source code. Judging by the pseudocode in the prior section, cpf appears to be simply an array of 11 integers.

EDIT: There are a few code samples on Google in various languages.

ColdFusion: http://www.adobe.com/cfusion/exchange/index.cfm?event=extensionDetail&loc=en_us&extid=1000248

VB: http://www.planetsourcecode.com/vb/scripts/ShowCode.asp?txtCodeId=3811&lngWId=10

These look more complete and might be easier for you to understand.


It appears that cpf is an array of integers, with 11 items.

Each is accessed by index, so the first one is - cpf[0], the second cpf[1] etc...

v1 and v2 are declared in the function and are assigned the values of calculations.

The last line compares these calculated values to the two last array items in cpf, presumably check digits.

All in all this code looks like it performs a calculation on a the CPF number and checking if the CPF is valid.


The equation is basically a very simple hash function which results in two "check digits."

This check digit "hash" can be used to verify the validity of the 9-digit number that the check digits are appended to much in the same way as MD5 and SHA1 hashes can be associated with files and used to verify that the proper data was transmitted or that no tampering had taken place before, during or after transmital.

The mathematics in this case are somewhat simple and arbitrary but will yield the same answer for the same input every time. You could tweak the equations many different ways and end up with valid check digits, as long as the same equations are used every time.


This is an algorithm for determining the validity of a number. Credit cards use a similar algorithm (Luhn). The idea is to prevent some permutations of the set of all combinations as a way of preventing fraud, and to allow validation and early opt-out: "could this POSSIBLY be a valid number for this?"

Using that method, you can determine if the number is in the valid format, if it is, you can try using it. If not, you shouldn't, as it's clearly not valid.

See this link about credit card numbers for a something similar: http://www.merriampark.com/anatomycc.htm

In addition "A[N]" means "the Nth element of A, where A is an array of some elements." cpf appears to be an array of integers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜