def inn_ctrl_summ(nums, type):
"""
Подсчет контрольной суммы
"""
inn_ctrl_type = {
'n2_12': [7, 2, 4, 10, 3, 5, 9, 4, 6, 8],
'n1_12': [3, 7, 2, 4, 10, 3, 5, 9, 4, 6, 8],
'n1_10': [2, 4, 10, 3, 5, 9, 4, 6, 8],
}
n = 0
l = inn_ctrl_type[type]
for i in range(0, len(l)):
n += nums[i] * l[i]
return n % 11 % 10
def inn_gen(l=None):
"""
Генерация ИНН (10 или 12 значный)
На входе указывается длина номера - 10 или 12.
Если ничего не указано, будет выбрана случайная длина.
"""
if not l:
l = list((10, 12))[rnd(0, 1)]
if l not in (10, 12):
return None
nums = [
rnd(1, 9) if x == 0
else rnd(0, 9)
for x in range(0, 9 if l == 10 else 10)
]
if l == 12:
n2 = inn_ctrl_summ(nums, 'n2_12')
nums.append(n2)
n1 = inn_ctrl_summ(nums, 'n1_12')
nums.append(n1)
elif l == 10:
n1 = inn_ctrl_summ(nums, 'n1_10')
nums.append(n1)
return ''.join([str(x) for x in nums])
def inn_check(inn):
"""
Проверка ИНН на корректность
В соответствии с алгоритмом, описанным по ссылке:
https://ru.wikipedia.org/wiki/Контрольное_число
"""
sinn = str(inn)
nums = [int(x) for x in sinn]
if len(sinn) == 10:
n1 = inn_ctrl_summ(nums, 'n1_10')
return n1 == nums[-1]
elif len(sinn) == 12:
n2 = inn_ctrl_summ(nums, 'n2_12')
n1 = inn_ctrl_summ(nums, 'n1_12')
return n2 == nums[-2] and n1 == nums[-1]
else:
return False
function INN(const INN: string): Boolean;
const
factor1: array[0..8] of byte = (2, 4, 10, 3, 5, 9, 4, 6, 8);
factor2: array[0..9] of byte = (7, 2, 4, 10, 3, 5, 9, 4, 6, 8);
factor3: array[0..10] of byte = (3, 7, 2, 4, 10, 3, 5, 9, 4, 6, 8);
var
i: byte;
sum: word;
sum2: word;
begin
Result := False;
try
if Length(INN) = 10 then begin
sum := 0;
for i := 0 to 8 do
sum := sum + StrToInt(INN[i + 1]) * factor1[i];
sum := sum mod 11;
sum := sum mod 10;
Result := StrToInt(INN[10]) = sum;
end
else if Length(INN) = 12 then begin
sum := 0;
for i := 0 to 9 do
sum := sum + StrToInt(INN[i + 1]) * factor2[i];
sum := sum mod 11;
sum := sum mod 10;
sum2 := 0;
for i := 0 to 10 do
sum2 := sum2 + StrToInt(INN[i + 1]) * factor3[i];
sum2 := sum2 mod 11;
sum2 := sum2 mod 10;
Result := (StrToInt(INN[11]) = sum) and
(StrToInt(INN[12]) = sum2);
end; //
except
Result := False;
end; // try
end;
Option Explicit
Option Base 1
'123456789012345678901234567890123456h8nor@ya567890123456789012345678toster56789
Function CRC_ITN(ByVal ITN12orTIN10 As Double) As Boolean ' Проверка ИНН
Dim FactorSum() As Variant, i As Integer, n As Byte, s As Integer
FactorSum = Array(3, 7, 2, 4, 10, 3, 5, 9, 4, 6, 8)
n = Len(CStr(ITN12orTIN10)) ' По длине определяем: Физ или Юр лицо
If n - 1 > UBound(FactorSum) Then Exit Function ' False, если больше 12 цифр
For i = n - 1 To 1 Step -1
s = s + CByte(Mid(ITN12orTIN10, i, 1)) * FactorSum(12 - n + i)
Next i: i = (s \ 11) * 11
If Right(ITN12orTIN10, 1) = Right(s - i, 1) Then CRC_ITN = True
If n = 12 Then If Not CRC_ITN(Left(ITN12orTIN10, n - 1)) Then CRC_ITN = False
End Function
def generate_personal_inn():
nums = [random.randint(1, 9) if x == 0 else random.randint(0, 9) for x in range(0, 10)]
weights = [
[7, 2, 4, 10, 3, 5, 9, 4, 6, 8],
[3, 7, 2, 4, 10, 3, 5, 9, 4, 6, 8]
]
nums.append(sum([n * w for w, n in zip(weights[0], nums)]) % 11 % 10)
nums.append(sum([n * w for w, n in zip(weights[1], nums)]) % 11 % 10)
return ''.join(map(str, nums))