Skip to content
with기술 정리

Encrypting passwords with crypto-js (SHA256)

1 min readTech notesMoved from my Tistory blogRead the Korean original

At work, the back end asked me to encrypt the password before sending it when creating a member. When I asked my senior, I heard that our company hashes the password with crypto-js before passing it on, so I came home and practiced it once more.

First, run the code below.

bash
npm install crypto-js
bash
npm install --save @types/crypto-js

Next, the practice code is below.

jsx
import React, { useState } from "react";

import { SHA256 } from "crypto-js";

export default function HashPwd() {
  const [pwd, setPwd] = useState("");
  const password = pwd;
  const hashedPassword = SHA256(password).toString();

  const handleSubmit = () => {
    console.log(hashedPassword);
  };
  return (
    <>
      <input type="password" onChange={(e) => setPwd(e.target.value)} />
      <button type="submit" onClick={() => handleSubmit()}>
        제출
      </button>
    </>
  );
}
  • At work I installed it with yarn, but at home it was a React project made with Vite, so there was a JavaScript version difference. I used an import statement instead of a require statement.
Password encryption
Figure 1. Password encryption

I learned one more thing today!

  • #crypto-js
  • #import
  • #require
  • #SHA256
  • #Encryption

More in Tech notes