簡體   English   中英

將 python 簽名代碼轉換為 javascript

[英]Convert python signing code to javascript

我目前正在研究代碼生成器並將其移植到 javascript,但是我似乎無法獲得相同的結果?

我在 Python 中得到的代碼如下:

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: %i\tCharacter %i:%s\t\tModulus %i\t\tModulus Code %i:%s\t\tNew 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("\r\nTarget 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

這是我在 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);
}

在 Javascript 中運行它時,我只是收到大量"Unprintable character =>結果被拋出?

您的問題似乎是您從此 Python 代碼轉換

new_char = chr(new_code)

至 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工作,因為這不是Number類型可用的方法。

如果代碼在catch塊中記錄了實際error ,那就很清楚了——異常的性質應該表明TypeError: new_code.charCodeAt is not a function

您可能正在尋找String.fromCharCode ,因此有問題的行應該是:

new_char = String.fromCharCode(new_code);

如果您使用 GitHub Copilot 生成代碼,您將得到以下內容。

請注意代碼如何使用String.fromCharCode(new_code)而不是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}\t\tModulus ${index % api_length}\t\tModulus Code ${temp_code_2}:${String.fromCharCode(temp_code_2)}\t\tNew 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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM