-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
98e2c36
commit 41c9f46
Showing
3 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
body { | ||
font-family: Arial, sans-serif; | ||
background-color: #f4f4f4; | ||
margin: 0; | ||
padding: 0; | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
height: 100vh; | ||
} | ||
|
||
#container { | ||
background-color: #fff; | ||
border-radius: 8px; | ||
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); | ||
padding: 20px; | ||
width: 800px; | ||
height: 500px; | ||
text-align: center; | ||
} | ||
|
||
label { | ||
display: block; | ||
margin-bottom: 8px; | ||
font-weight: bold; | ||
} | ||
|
||
input { | ||
width: 100%; | ||
padding: 8px; | ||
margin-bottom: 16px; | ||
box-sizing: border-box; | ||
} | ||
|
||
button { | ||
background-color: #007BFF; | ||
color: #fff; | ||
border: none; | ||
padding: 10px 20px; | ||
font-size: 16px; | ||
cursor: pointer; | ||
border-radius: 4px; | ||
} | ||
|
||
button:hover { | ||
background-color: #0056b3; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>ECDSA Signature Generator</title> | ||
<link rel="stylesheet" href="./css/ecdsa2.css"> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/elliptic/6.5.3/elliptic.min.js"></script> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.js"></script> | ||
</head> | ||
|
||
<body> | ||
<div id="container"> | ||
<h2>ECDSA Signature Generator</h2> | ||
<label for="privateKey">Private Key:</label> | ||
<input type="text" id="privateKey" placeholder="Enter private key"> | ||
|
||
<label for="message">Message:</label> | ||
<input type="text" id="message" placeholder="Enter message"> | ||
|
||
<button id="generateBtn">Generate Signature</button> | ||
|
||
<h3>Signature (r, s):</h3> | ||
<p id="signatureOutput"></p> | ||
|
||
<h3>Signature Verification:</h3> | ||
<p id="verificationOutput"></p> | ||
</div> | ||
|
||
<script src="./js/ecdsa.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
document.getElementById('generateBtn').addEventListener('click', generateSignature); | ||
|
||
function generateSignature() { | ||
const privateKeyHex = document.getElementById('privateKey').value; | ||
const message = document.getElementById('message').value; | ||
|
||
// Validate input | ||
if (!privateKeyHex || !message) { | ||
alert('Please enter both private key and message.'); | ||
return; | ||
} | ||
|
||
try { | ||
// Convert the private key from hex to bytes | ||
const privateKeyBytes = hexToBytes(privateKeyHex); | ||
|
||
// Create an elliptic curve object using the secp256k1 curve | ||
const ec = new elliptic.ec('secp256k1'); | ||
|
||
// Create a key pair from the private key | ||
const key = ec.keyFromPrivate(privateKeyBytes); | ||
|
||
// Hash the message using SHA-256 | ||
const hashedMessage = CryptoJS.SHA256(CryptoJS.enc.Utf8.parse(message)).toString(CryptoJS.enc.Hex); | ||
|
||
// Sign the hashed message | ||
const signature = key.sign(hashedMessage); | ||
|
||
/* Modify the signature to make it invalid (for example, increment s) | ||
signature.s = signature.s.add(ec.n); // This is just an example, you can modify it differently*/ | ||
|
||
// Display the signature in hex format | ||
const signatureOutput = `(${signature.r.toString(16)}, ${signature.s.toString(16)})`; | ||
document.getElementById('signatureOutput').innerText = signatureOutput; | ||
|
||
// Verification logic | ||
const validSignature = key.verify(hashedMessage, signature); | ||
|
||
// Display the verification result | ||
const verificationOutput = `Is Signature valid? : ${validSignature}`; | ||
document.getElementById('verificationOutput').innerText = verificationOutput; | ||
} catch (error) { | ||
alert('Error: ' + error.message); | ||
} | ||
} | ||
|
||
// Hex to Bytes conversion | ||
function hexToBytes(hex) { | ||
const bytes = []; | ||
for (let i = 0; i < hex.length; i += 2) { | ||
bytes.push(parseInt(hex.substr(i, 2), 16)); | ||
} | ||
return new Uint8Array(bytes); | ||
} |