定义哈希编码SHA256 NodeJS

时间:2019-07-09 04:34:48

标签: node.js sha256 sha

如何在NodeJS上选择哈希码?

我有一个使用其他语言制作的系统,其密码在SHA256上

其中的功能是:

#define HASH_CODE = 'WEASDSAEWEWAEAWEAWEWA';
SHA256_PassHash(HASH_CODE, password, 64);

首先,param是哈希码,其次是将要加密的var,第三是base64

我在NodeJS上进行了加密,但是我无法控制哈希码,因此系统不会进行相同的哈希,如何在NodeJS上的寄存器上选择代码,以便系统与其他人通信?

const code = 'WEASDSAEWEWAEAWEAWEWA';
const normal = 'anne';
const crypto = require('crypto');
const encrypted = crypto
     .createHash('sha256')
     .update(normal)
     .digest('base64');
console.log(encrypted);

一个兼容代码的示例,此登录名是PHP login.php

<?php require_once('../mysql_conn.php'); ?> 
    <?php
    session_start();
    $HASH_SENHA = 'WEASDSAEWEWAEAWEAWEWA';

    if(isset($_SESSION['Username']))
    {
        header("location: ../myaccount.php");
        exit();
    } 
    if(isset($_POST['usr']) && isset($_POST['psw']) && isset($_POST['botao'])) 
    { 
        $usuario = mysqli_real_escape_string($MYSQL_CONNECT, $_POST['usr']);  
        $senha = strtoupper(hash("sha256", $_POST['psw'] . $HASH_SENHA));  
        $query = mysqli_query($MYSQL_CONNECT, "SELECT * FROM accounts WHERE Username='$usuario' AND Senha='$senha' LIMIT 1");  

        if(mysqli_num_rows($query) < 1)  
        {
            echo "<script type=\"text/javascript\">
                        alert('Incorrect Username or Password.');
                        window.location = '../login.php';
                  </script>";
            exit();
        }
        else 
        {
            //login efetuado 
            $dados = mysqli_fetch_assoc($query);  


            if (isset($_SESSION['loc'])) {
                header("location:".$_SESSION['loc']);
            }
            else header("location:../index.php");

        }  
    }
    ?> 

1 个答案:

答案 0 :(得分:0)

通过查看您提供的PHP代码。

hash("sha256", $_POST['psw'] . $HASH_SENHA)

正在哈希$_POST['psw']$HASH_SENHA的字符串串联

因此,Node.js中的等效代码应如下所示

Node.js

const crypto = require('crypto');
const code = 'WEASDSAEWEWAEAWEAWEWA';
const input = 'password 123';

const encrypted = crypto
     .createHash('sha256')
     .update(input + code)  // concatenation
     .digest('hex');        // get hash in hex format

console.log(encrypted);

输出

3b3107f01da624da6bb014abe532aa7416869811ebe321784b26770cd2dd74ff