Convert python signing code to javascript
Question:
I’m currently working on a code generator and porting it over to javascript, however I can’t seem to get the same results?
The code which I’ve got in Python is the following:
def sign(self, cs, api):
temp_string = ""
cs_length = len(cs)
api_length = len(api)
if api_length > 0:
for index in range(0, cs_length):
temp_char = cs[index]
temp_code = ord(temp_char)
temp_code_2 = ord(api[index % api_length])
new_code = self.get_code(temp_code, 47, 57, temp_code_2)
new_char = ""
if new_code != cs[index]:
new_char = chr(new_code)
try:
temp_string += new_char
except:
xbmc.log("Unprintable character => %s" % new_char)
xbmc.log("Index: %itCharacter %i:%sttModulus %ittModulus Code %i:%sttNew Code %i:%s" %
(index, temp_code, temp_char, (index % api_length), temp_code_2, chr(temp_code_2), new_code,
new_char))
else:
xbmc.log("Signature cannot be blank")
# xbmc.log("rnTarget Signature: 7a74G7m23Vrp0o5c")
xbmc.log("Result signature: %s" % temp_string[0:16])
return temp_string[0:16]
def get_code(self, char_code, const_1, const_2, char_result):
if const_1 < char_code <= const_2:
char_code += char_result % (const_2 - const_1)
if char_code > const_2:
char_code = char_code - const_2 + const_1
return char_code
And here’s what I’ve wrote in Javascript:
get_code(char_code, const_1, const_2, char_result) {
if(const_1 < char_code <= const_2) {
char_code += char_result % (const_2 - const_1);
if(char_code > const_2) {
char_code = char_code - const_2 + const_1;
}
}
return char_code;
}
sign(cs, api) {
let temp_string = '';
let cs_length = cs.length;
let api_length = api.length;
if(api_length > 0) {
for(let index in this.range(0, cs_length)) {
let temp_char = cs[index];
let temp_code = temp_char.charCodeAt(0);
let temp_code_2 = api[index % api_length].charCodeAt(0);
let new_code = this.get_code(temp_code, 47, 57, temp_code_2);
let new_char = "";
if(new_code != cs[index]) {
try {
new_char = new_code.charCodeAt(0);
temp_string += new_char;
} catch(error) {
console.log('"Unprintable character => ' + new_char);
}
}
}
}
return temp_string.substring(0, 16);
}
When running it in Javascript, I’m just getting a ton of "Unprintable character =>
results being thrown?
Answers:
Your issue appears to be your conversion from this Python code
new_char = chr(new_code)
To JavaScript
new_char = new_code.charCodeAt(0);
Given that chr
in Python takes an int
and return a single length str
, while String.charCodeAt
does the exact opposite – also given that the value of new_code
appears to be an Number
or int
(in JavaScript and Python respectively), that will simply not work as that is not a method available for the Number
type.
It would have been clear had the code logged the actual error
inside the catch
block – the nature of the exception should have revealed that TypeError: new_code.charCodeAt is not a function
.
You were probably looking for String.fromCharCode
, so that problematic line should be:
new_char = String.fromCharCode(new_code);
If you use GitHub Copilot to generate the code, you will get the following.
Notice how the code uses String.fromCharCode(new_code)
instead of new_code.charCodeAt(0)
.
function sign(cs, api) {
let temp_string = "";
let cs_length = cs.length;
let api_length = api.length;
if (api_length > 0) {
for (let index = 0; index < cs_length; index++) {
let temp_char = cs[index];
let temp_code = ord(temp_char);
let temp_code_2 = ord(api[index % api_length]);
let new_code = get_code(temp_code, 47, 57, temp_code_2);
let new_char = "";
if (new_code != cs[index]) {
new_char = String.fromCharCode(new_code);
try {
temp_string += new_char;
} catch (e) {
console.log("Unprintable character => " + new_char);
}
}
console.log(`Index: ${index}tCharacter ${temp_code}:${temp_char}ttModulus ${index % api_length}ttModulus Code ${temp_code_2}:${String.fromCharCode(temp_code_2)}ttNew Code ${new_code}:${new_char}`);
}
} else {
console.log("Signature cannot be blank");
}
console.log(`Result signature: ${temp_string.substring(0, 16)}`);
return temp_string.substring(0, 16);
}
function get_code(char_code, const_1, const_2, char_result) {
if (const_1 < char_code && char_code <= const_2) {
char_code += char_result % (const_2 - const_1);
if (char_code > const_2) {
char_code = char_code - const_2 + const_1;
}
}
return char_code;
}
I’m currently working on a code generator and porting it over to javascript, however I can’t seem to get the same results?
The code which I’ve got in Python is the following:
def sign(self, cs, api):
temp_string = ""
cs_length = len(cs)
api_length = len(api)
if api_length > 0:
for index in range(0, cs_length):
temp_char = cs[index]
temp_code = ord(temp_char)
temp_code_2 = ord(api[index % api_length])
new_code = self.get_code(temp_code, 47, 57, temp_code_2)
new_char = ""
if new_code != cs[index]:
new_char = chr(new_code)
try:
temp_string += new_char
except:
xbmc.log("Unprintable character => %s" % new_char)
xbmc.log("Index: %itCharacter %i:%sttModulus %ittModulus Code %i:%sttNew Code %i:%s" %
(index, temp_code, temp_char, (index % api_length), temp_code_2, chr(temp_code_2), new_code,
new_char))
else:
xbmc.log("Signature cannot be blank")
# xbmc.log("rnTarget Signature: 7a74G7m23Vrp0o5c")
xbmc.log("Result signature: %s" % temp_string[0:16])
return temp_string[0:16]
def get_code(self, char_code, const_1, const_2, char_result):
if const_1 < char_code <= const_2:
char_code += char_result % (const_2 - const_1)
if char_code > const_2:
char_code = char_code - const_2 + const_1
return char_code
And here’s what I’ve wrote in Javascript:
get_code(char_code, const_1, const_2, char_result) {
if(const_1 < char_code <= const_2) {
char_code += char_result % (const_2 - const_1);
if(char_code > const_2) {
char_code = char_code - const_2 + const_1;
}
}
return char_code;
}
sign(cs, api) {
let temp_string = '';
let cs_length = cs.length;
let api_length = api.length;
if(api_length > 0) {
for(let index in this.range(0, cs_length)) {
let temp_char = cs[index];
let temp_code = temp_char.charCodeAt(0);
let temp_code_2 = api[index % api_length].charCodeAt(0);
let new_code = this.get_code(temp_code, 47, 57, temp_code_2);
let new_char = "";
if(new_code != cs[index]) {
try {
new_char = new_code.charCodeAt(0);
temp_string += new_char;
} catch(error) {
console.log('"Unprintable character => ' + new_char);
}
}
}
}
return temp_string.substring(0, 16);
}
When running it in Javascript, I’m just getting a ton of "Unprintable character =>
results being thrown?
Your issue appears to be your conversion from this Python code
new_char = chr(new_code)
To JavaScript
new_char = new_code.charCodeAt(0);
Given that chr
in Python takes an int
and return a single length str
, while String.charCodeAt
does the exact opposite – also given that the value of new_code
appears to be an Number
or int
(in JavaScript and Python respectively), that will simply not work as that is not a method available for the Number
type.
It would have been clear had the code logged the actual error
inside the catch
block – the nature of the exception should have revealed that TypeError: new_code.charCodeAt is not a function
.
You were probably looking for String.fromCharCode
, so that problematic line should be:
new_char = String.fromCharCode(new_code);
If you use GitHub Copilot to generate the code, you will get the following.
Notice how the code uses String.fromCharCode(new_code)
instead of new_code.charCodeAt(0)
.
function sign(cs, api) {
let temp_string = "";
let cs_length = cs.length;
let api_length = api.length;
if (api_length > 0) {
for (let index = 0; index < cs_length; index++) {
let temp_char = cs[index];
let temp_code = ord(temp_char);
let temp_code_2 = ord(api[index % api_length]);
let new_code = get_code(temp_code, 47, 57, temp_code_2);
let new_char = "";
if (new_code != cs[index]) {
new_char = String.fromCharCode(new_code);
try {
temp_string += new_char;
} catch (e) {
console.log("Unprintable character => " + new_char);
}
}
console.log(`Index: ${index}tCharacter ${temp_code}:${temp_char}ttModulus ${index % api_length}ttModulus Code ${temp_code_2}:${String.fromCharCode(temp_code_2)}ttNew Code ${new_code}:${new_char}`);
}
} else {
console.log("Signature cannot be blank");
}
console.log(`Result signature: ${temp_string.substring(0, 16)}`);
return temp_string.substring(0, 16);
}
function get_code(char_code, const_1, const_2, char_result) {
if (const_1 < char_code && char_code <= const_2) {
char_code += char_result % (const_2 - const_1);
if (char_code > const_2) {
char_code = char_code - const_2 + const_1;
}
}
return char_code;
}