Spaces:
Sleeping
Sleeping
File size: 1,992 Bytes
e4ae666 8151130 7a612b3 8151130 7a612b3 8151130 7a612b3 8151130 e4ae666 7a612b3 e4ae666 7a612b3 e4ae666 7a612b3 e4ae666 7a612b3 e4ae666 7a612b3 e4ae666 7a612b3 e4ae666 7a612b3 e4ae666 8151130 e4ae666 7a612b3 e4ae666 7a612b3 e4ae666 7a612b3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | const express = require("express");
const axios = require("axios");
const crypto = require("crypto");
const fs = require("fs");
const app = express();
app.use(express.json());
const PORT = process.env.PORT || 8080;
let apiCount = 0;
// Load the API count from a file on server start
fs.readFile("apiCount.txt", "utf8", (err, data) => {
if (!err) {
apiCount = parseInt(data);
}
});
// Endpoint to check if a password is breached
app.post("/checkPassword", async (req, res) => {
try {
apiCount++;
const password = req.body.password;
if (!password) {
return res.status(400).send("Password is required");
}
// Hash the password using SHA-1
const sha1Hash = crypto
.createHash("sha1")
.update(password)
.digest("hex")
.toUpperCase();
// Get the first 5 characters (prefix) of the hash
const prefix = sha1Hash.substring(0, 5);
// Query the Pwned Passwords API
const response = await axios.get(
`https://api.pwnedpasswords.com/range/${prefix}`
);
// Check if the password hash is in the response
const suffixes = response.data.split("\r\n");
let breached = false;
let breachCount = 0;
suffixes.forEach((suffix) => {
const [hashSuffix, count] = suffix.split(":");
if (prefix + hashSuffix === sha1Hash) {
breached = true;
breachCount = parseInt(count);
}
});
// Return the result with api count
if (breached) {
res.json({ breached: true, breachCount, apiCount });
} else {
res.json({ breached: false, apiCount });
}
// Save the updated API count to the file
fs.writeFile("apiCount.txt", apiCount.toString(), (err) => {
if (err) {
console.error("Error saving API count:", err);
}
});
} catch (error) {
console.error("Error:", error);
res.status(500).send("Internal Server Error");
}
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
|