計算統一社會信用代碼的校驗碼(Lua)

輸入

法人和其他組織統一社會信用代碼的前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))

參考

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章