How to reproduce System.Security.Cryptography.SHA1Managed result in Python

后端 未结 5 679
梦毁少年i
梦毁少年i 2021-01-17 03:07

Here\'s the deal: I\'m moving a .NET website to Python. I have a database with passwords hashed using the System.Security.Cryptography.SHA1Managed utility.

I\'m cre

5条回答
  •  暖寄归人
    2021-01-17 03:52

    Thanks @Leo Muler , your csharp code helped me a lot to translate it into nodejs.

    Here is the code :

       const saltLength = 16;
    const cryptedPwd = 'm2gFufL1WYJEcjdgnu4Eo0qXHM8+whC75AMnYxCS+uRbiS4OBy5+4TKNQbiSJyTG';
    const pwd = 'myPassword';
    
    let binaryPwd = Buffer.from(cryptedPwd, 'base64');
    
    let salt = binaryPwd.slice(0, saltLength);
    let saltBuffer = [...salt];
    let bytePwd = Buffer.from(pwd, 'utf16le');
    let pwdBuffer = [...bytePwd];
    let saltAndPwd = saltBuffer.concat(pwdBuffer);
    let saltAndPwdBinary = Buffer.from(saltAndPwd).toString('utf16le');
    let cryptedBuffer = Array.from(crypto.createHash('sha256').update(saltAndPwdBinary, 'utf16le').digest());
    let concatCryptedBuffer = saltBuffer.concat(cryptedBuffer);
    let cryptedString = Buffer.from(concatCryptedBuffer).toString('base64');
    
    console.log('cryptedString : ' + cryptedString);
    console.log('same : ' + (cryptedString == cryptedPwd));
    console.log('');
    

提交回复
热议问题