factumovil
Version:
Utilities for Bolivian invoice generation
109 lines (100 loc) • 2.9 kB
JavaScript
function verhoeff(num, times)
{
d = [
[],
[],
[],
[],
[],
[],
[],
[],
[],
[]
]
p = [
[],
[],
[],
[],
[],
[],
[],
[]
]
inv = [0, 4, 3, 2, 1, 5, 6, 7, 8, 9]
for (;times > 0; times--) {
c = 0
for (i = num.length-1; i >= 0; i--){
c = d[c][p[((num.length - i) % 8)][num[i]]]
}
num += inv[c]
}
return num
}
function arc4(msg, key)
{
state = []
for (i=0; i<256; i++) {
state[i] = i
}
j = 0
for (i=0; i<256; i++) {
j = (j + state[i] + key[i % key.length].charCodeAt(0)) % 256
temp = state[i]
state[i] = state[j]
state[j] = temp
}
x = 0, y = 0
output = ""
for (i=0; i<msg.length; i++) {
x = (x + 1) % 256
y = (state[x] + y) % 256
temp = state[x]
state[x] = state[y]
state[y] = temp
output += ("0" + (msg[i].charCodeAt(0) ^ state[(state[x] + state[y]) % 256]).toString(16)).slice(-2)
}
return output.toUpperCase();
}
function base64(number) {
result = ""
dic = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/"
while (number > 0) {
result = dic[number % 64] + result
number = Math.floor(number / 64)
}
return result
}
function controlCode(auth, number, nit, date, total, key) {
code = ""
number = verhoeff(number, 2)
nit = verhoeff(nit, 2)
date = verhoeff(date, 2)
total = verhoeff(total, 2)
vf = verhoeff("" + (
parseInt(number) +
parseInt(nit) +
parseInt(date) +
parseInt(total))
, 5).slice(-5)
input = [auth, number, nit, date, total]
idx = 0;
for (i=0; i<5; i++) {
code += input[i] + key.substring(idx, idx+1+parseInt(vf[i]))
idx += 1+parseInt(vf[i])
}
code = arc4(code, key + vf)
final_sum = 0
total_sum = 0
partial_sum = [0,0,0,0,0]
for (i=0; i<code.length; i++) {
partial_sum[i%5] += code[i].charCodeAt(0)
total_sum += code[i].charCodeAt(0)
}
for (i=0; i<5; i++) {
final_sum += Math.floor((total_sum * partial_sum[i]) / (1 + parseInt(vf[i])))
}
code = arc4(base64(final_sum), key + vf).match(/.{2}/g).join("-")
return code
}