输入
法人和其他组织统一社会信用代码的前16/17位
输出
检查登记管理部门代码(第1位)和机构类别代码(第2位)是否合法;
检查登记管理机关行政区划码(第3-8位)是否合法;
检查组织机构代码的本体代码(第9-16位)是否合法;
如果输入为17位,则检查组织机构代码的校验码(第17位)是否正确;
输出完整的统一社会信用代码,包括组织机构代码的校验码(第17位)和统一代码的校验码(第18位)。
Lua实现代码
考虑到登记管理部门代码标识和机构类别代码标识的调整,将这部分内容放到Lua脚本中配置以方便修改。
如有调整,则修改 legalRegCodes 和 legalClassCodes 对应的值。
local legalRegCodes = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "N", "Y" }
local legalClassCodes = {
"11", "12", "13", "19",
"21", "29",
"31", "32", "33", "34", "35", "39",
"41", "49",
"51", "52", "53", "59",
"61", "62", "69",
"71", "72", "79",
"81", "89",
"91", "92", "93",
"A1", "A9",
"N1", "N2", "N3", "N9",
"Y1" }
--[[ Return
- nil if the value is not in the table, or
- the index number (begin from 1) of the value ]]
local function GetIndex(table, value)
for i, v in ipairs(table) do
if v == value then
return i
end
end
return nil
end
function checkRegCode(inRegCode)
if inRegCode and GetIndex(legalRegCodes, inRegCode) then
return true
end
return false
end
function checkClassCode(inClassCode)
if (inClassCode and GetIndex(legalClassCodes, inClassCode)) then
return true
end
return false
end
function checkDivisionCode(inDivisionCode)
local dc = tonumber(inDivisionCode)
if (dc and 110000 <= dc and dc <= 829999) then
return true
end
return false
end
--[[ Return
- nil if inOrgCodeMaster is invalid, or
- the checksum string of inOrgCodeMaster ]]
function GetOrgCodeChecksum(inOrgCodeMaster)
local legalOrgCodeSet = {
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
"K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
"U", "V", "W", "X", "Y", "Z" }
local w = { 3, 7, 9, 10, 5, 8, 4, 2 }
local s = 0;
for i = 1, 8, 1 do
local k = GetIndex(legalOrgCodeSet, string.sub(inOrgCodeMaster, i, i))
if k then
s = s + (k - 1) * w[i]
else
return nil
end
end
local r = s % 11;
if r == 0 then
return '0'
end
if r == 1 then
return 'X'
end
return tostring(11 - r)
end
function GetUIDChecksum(inID)
local legalCodes = {
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"A", "B", "C", "D", "E", "F", "G", "H", "J", "K",
"L", "M", "N", "P", "Q", "R", "T", "U", "W", "X", "Y" }
local w = { 1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28 }
local s = 0
for i = 1, 16, 1 do
s = s + (GetIndex(legalCodes, string.sub(inID, i, i)) - 1) * w[i]
end
local OIDChecksum = GetOrgCodeChecksum(string.sub(inID, 9, 16))
s = s + (GetIndex(legalCodes, OIDChecksum) - 1) * w[17]
return legalCodes[32 - (s % 31)]
end
-- main
print "Enter the master number (first 16 or 17 bytes) of the unified identifier: "
local inID = io.read('*L')
-- check size
local len = string.len(inID)
if (len < 17 or 18 < len) then
print "\nThe size of the master number is incorrect."
return nil
end
-- check registration management code (1st byte)
local inRegCode = string.sub(inID, 1, 1)
if not checkRegCode(inRegCode) then
print "\nThe registration management code is incorrect:"
print (inID)
print "^"
return nil
end
-- check institutional class code (2nd byte)
local inClassCode = string.sub(inID, 1, 2)
if not checkClassCode(inClassCode) then
print "\nThe institutional class code is incorrect:"
print (inID)
print " ^"
return nil
end
-- check administrative division code (3rd-8th bytes)
local inDivisionCode = string.sub(inID, 3, 8)
if not checkDivisionCode(inDivisionCode) then
print "\nThe administrative division code is incorrect:"
print (inID)
print " ^^^^^^"
return nil
end
-- check organization code (9th-16th bytes)
local inOrgCodeMaster = string.sub(inID, 9, 16)
local OIDChecksum = GetOrgCodeChecksum(inOrgCodeMaster)
if not OIDChecksum then
print "\nThe master number of the organization code is incorrect:"
print (inID)
print " ^^^^^^^^"
return nil
end
-- check organization code checksum (17th byte)
if string.len(inID) > 17 then
local inOrgCodeChecksum = string.sub(inID, 17, 17)
if inOrgCodeChecksum ~= OIDChecksum then
print "\nThe organization code checksum is incorrect:"
print (inID)
print " ^"
return nil
end
end
-- good
print ("\nThe unified identifier is: ",
inClassCode, inDivisionCode, inOrgCodeMaster, OIDChecksum, GetUIDChecksum(inID))