简体   繁体   中英

How to convert HMAC_SHA256_MAC in PHP

index.php

<form class="row g-3" action="<?php echo base_url('signin'); ?>" method="post" id="loginForm" onsubmit="return hashpass();"> 
    <div class="col-12">
        <label for="inputUsername" class="form-label">Username</label>
            <input type="text" class="form-control" name="uname" id="uname" placeholder="Enter Username" required>
        </div>
        <div class="col-12">
        <label for="inputChoosePassword" class="form-label">Password</label>
        <div class="input-group" id="show_hide_password">
            <input type="password" class="form-control border-end-0" name="password" required id="password" value="12345678" placeholder="Enter Password"> <a href="javascript:;" class="input-group-text bg-transparent"><i class='bx bx-hide'></i></a>
        </div>
    </div>

    <div class="col-12">
        <div class="d-grid">
            <button type="submit" id="signin" class="btn btn-primary" onclick="hashpass();"><i class="bx bxs-lock-open"></i>Sign in</button>
        </div>
    </div>
</form>
<script src="<?php echo base_url(); ?>/assets/js/HmacSHA256.js"></script>
<script>
function hashpass(){
     var FormName='loginForm';
     document.forms[FormName]["password"].value=HMAC_SHA256_MAC("aSm0$i_20eNh3os", document.forms[FormName]["password"].value);
            return true;
 }
</script>

Home_Controller.php

public function signin()
    { 
        $userModel = new UserModel();
        $uname = $this->request->getPost('uname');
        $password = $this->request->getPost('password');
        $data = $userModel->signin($uname);
        
        foreach($data as $key=>$value)
        {           
            $auth_id=$value->auth_id;
            $s_password=$value->password;
        }
        
        if(!empty($data))
        { 
            if(password_verify($password, $s_password))
            {   
                $session->set('auth_id',$auth_id);
                $this->response->redirect(base_url('dashboard'));
            }else{ 
                $this->response->redirect(base_url());
            }   
        }
        else
        {
            $this->response->redirect(base_url());
        }
    }

If I get value from front end suppose value is demo enter from front end then get in controller If I print $2y$10$nxdb2IIIu0QMcXH0T0q0buKcugSUNT3muDOmelBO.fjTE5D8OrICq . So now without this script I want $2y$10$nxdb2IIIu0QMcXH0T0q0buKcugSUNT3muDOmelBO.fjTE5D8OrICq this output using PHP function.

Here is the php exmple.

<?php
echo hash_hmac('sha256', 'data', 'secret');
?>

Update the JS script

<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/hmac-sha256.js"></script>
<script>
function hashpass(){
     var FormName='loginForm';
     document.forms[FormName]["password"].value=CryptoJS.HmacSHA256(document.forms[FormName]["password"].value, "aSm0$i_20eNh3os");
            return true;
 }
</script>

You can check the link. Hash HMAC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM