Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix zero before sign result #280

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

mokgumyuen
Copy link

@mokgumyuen mokgumyuen commented Dec 7, 2022

public sign(text:string, digestMethod:(str:string) => string, digestName:string):string {
        const header = getDigestHeader(digestName);
        const digest = header + digestMethod(text).toString();
        const m = pkcs1pad1(digest, this.n.bitLength() / 4);
        if (m == null) {
            return null;
        }
        const c = this.doPrivate(m);
        if (c == null) {
            return null;
        }
        const h = c.toString(16);
        if ((h.length & 1) == 0) {
            return h;
        } else {
            return "0" + h;
        }
    }

h.length sometimes is 254 ,but the result should be 256

fix version:

public sign(text:string, digestMethod:(str:string) => string, digestName:string):string {
        const header = getDigestHeader(digestName);
        const digest = header + digestMethod(text).toString();
        let maxLength = this.n.bitLength() / 4;
        const m = pkcs1pad1(digest, maxLength);
        if (m == null) {
            return null;
        }
        const c = this.doPrivate(m);
        if (c == null) {
            return null;
        }

        let h = c.toString(16);
        let length = h.length

        // fix zero before result
        for (let i = 0; i < maxLength - length; i++) {
            h = "0" + h;
        }

        return h
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant