Skip to content

Instantly share code, notes, and snippets.

@froop
Created August 6, 2014 02:14
Show Gist options
  • Save froop/034806eeab339cfd9da9 to your computer and use it in GitHub Desktop.
Save froop/034806eeab339cfd9da9 to your computer and use it in GitHub Desktop.
[JS] 2-Step Password: パスワードを2段階にして覚えるのは単純な文字列で済むように http://blog.livedoor.jp/froo/archives/50906020.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>2-Step Password</title>
<script src="sha256.js"></script><!-- https://github.com/Caligatio/jsSHA -->
<script src="2StepPassword.js"></script>
</head>
<body>
<form id="main-form">
<input id="src-text" required>
<button type="submit">make</button>
</form>
<div>
<input id="res-text" readonly>
</div>
</body>
</html>
(function (global) {
var RESULT_LENGTH = 16;
function hashPassword(srcText) {
var base64 = new jsSHA(srcText, "ASCII").getHash("SHA-256", "B64");
return base64.replace(/[+/=]/g, "").substring(0, RESULT_LENGTH);
}
function onSubmit(event) {
event.preventDefault();
var srcText = document.getElementById("src-text").value;
var resText = hashPassword(srcText);
document.getElementById("res-text").value = resText;
}
function onLoad() {
var mainForm = document.getElementById("main-form");
mainForm.addEventListener("submit", onSubmit);
}
global.addEventListener("load", onLoad);
})(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment