Voice Authentication In Nodejs - Programming - Nairaland
Nairaland Forum › Science/Technology › Programming › Voice Authentication In Nodejs (303 Views)
| Voice Authentication In Nodejs by Alphabyte3(op): 3:42pm On Nov 28, 2024 |
Note that this code uses Node.js and several external libraries, including `whisper.js`, `sound-device`, `fs`, `path`, `bcryptjs`, and `js-yaml`. Credentials-settings.js const whisper = require('whisper.js'); const soundDevice = require('sound-device'); const fs = require('fs'); const path = require('path'); const bcrypt = require('bcryptjs'); const yaml = require('js-yaml'); // Load model const model = whisper.loadModel('small'); const fsSampleRate = 44100; // Sample rate const seconds = 3; // Duration of recording async function usernameSetup() { const usernameList = []; console.log("Please speak into the microphone your desired username 3 times." ;console.log("Now setting the username" ;for (let i = 0; i < 3; i++) { console.log(`This is recording number ${i + 1}`); const usernameRecording = await recordAudio(); await writeAudioToFile(usernameRecording, `username${i}.mp3`); const usernameTranscription = await transcribeAudio(`username${i}.mp3`); const output = usernameTranscription.toLowerCase().replace(/\W+/g, ''); usernameList.push(output); } return usernameList; } async function passSetup() { const passList = []; console.log("Please speak into the microphone your desired passphrase 3 times." ;console.log("Now setting up password" ;for (let i = 0; i < 3; i++) { console.log(`This is recording number ${i + 1}`); const passRecording = await recordAudio(); await writeAudioToFile(passRecording, `audio${i}.mp3`); const passTranscription = await transcribeAudio(`audio${i}.mp3`); const output = passTranscription.toLowerCase().replace(/\W+/g, ''); passList.push(output); } return passList; } function compareElements(list) { const element = list[0]; let check = true; for (const item of list) { if (element !== item) { check = false; break; } } return check; } async function database(username, pwd) { const hashedPwd = bcrypt.hashSync(pwd, 10); const data = { [username]: hashedPwd }; const filePath = path.join(__dirname, 'output.yaml'); if (!fs.existsSync(filePath)) { fs.writeFileSync(filePath, yaml.dump(data)); } else { const existingData = yaml.safeLoad(fs.readFileSync(filePath, 'utf8')); existingData[username] = hashedPwd; fs.writeFileSync(filePath, yaml.dump(existingData)); } return data; } // Helper functions async function recordAudio() { return new Promise((resolve, reject) => { const stream = soundDevice.open({ channels: 2, sampleRate: fsSampleRate, device: null }); const chunks = []; stream.on('data', (chunk) => { chunks.push(chunk); }); stream.on('end', () => { const audioBuffer = Buffer.concat(chunks); resolve(audioBuffer); }); stream.start(); setTimeout(() => { stream.stop(); }, seconds * 1000); }); } async function writeAudioToFile(audioBuffer, filename) { return new Promise((resolve, reject) => { fs.writeFile(filename, audioBuffer, (err) => { if (err) { reject(err); } else { resolve(); } }); }); } async function transcribeAudio(filename) { return new Promise((resolve, reject) => { model.transcribe(filename, (err, result) => { if (err) { reject(err); } else { resolve(result); } }); }); } // Call functions async function main() { const usernameList = await usernameSetup(); console.log(usernameList); if (compareElements(usernameList)) { console.log("Username set successfully" ;const passList = await passSetup(); console.log(passList); if (compareElements(passList)) { console.log("Password set successfully" ;await database(usernameList[0], passList[0]); console.log("User credentials saved successfully" ;} else { console.log("Password mismatch" ;} } else { console.log("Username mismatch" ;} } |
| Re: Voice Authentication In Nodejs by Alphabyte3(op): 3:43pm On Nov 28, 2024 |
Here is the JavaScript code. Note that this code uses the `bcryptjs` library, which is a JavaScript implementation of the bcrypt password hashing algorithm. hash_salt.py const bcrypt = require('bcryptjs'); // Hash and salt the plain password // Return the hashed one function hashSalt(pwd) { const salt = bcrypt.genSaltSync(); const hashed = bcrypt.hashSync(pwd, salt); return hashed; } // Example usage: const plainPassword = 'mysecretpassword'; const hashedPassword = hashSalt(plainPassword); console.log(hashedPassword); |
| Re: Voice Authentication In Nodejs by Alphabyte3(op): 3:47pm On Nov 28, 2024 |
Here is the JavaScript code: main.js const userSetup = require('./user_setup'); const verification = require('./verification'); const credentialsSetting = require('./credentials_setting'); // Call functions async function main() { await userSetup(); await verification(); } Note that this code assumes that you have the following files in the same directory: - `user_setup.js` - `verification.js` - `credentials_setting.js` Each of these files should export the corresponding functions. For example, `user_setup.js` might contain: ``` async function userSetup() { // User setup logic here } module.exports = userSetup; ``` Similarly, `verification.js` and `credentials_setting.js` should export the corresponding functions. |
| Re: Voice Authentication In Nodejs by Alphabyte3(op): 3:50pm On Nov 28, 2024 |
Here is the JavaScript code. Note that this code uses the `node-tts` library, which is a text-to-speech library for Node.js. system_speak.js const TTS = require('node-tts'); async function speak(text) { const tts = new TTS({ voice: 'en-US_ZiraNeural', rate: 150, }); await tts.speak(text); } // Example usage: speak('Hello, world!'); This code does the following: 1. Imports the `node-tts` library. 2. Defines a `speak` function that takes a text string as input. 3. Creates a new instance of the `TTS` class, specifying the voice and rate. 4. Uses the `speak` method to synthesize the text. 5. Provides an example usage of the `speak` function. Note that you will need to install the `node-tts` library using npm or yarn before running this code. You can do this by running the following command in your terminal: bash npm install node-tts |
| Re: Voice Authentication In Nodejs by Alphabyte3(op): 3:54pm On Nov 28, 2024*. Modified: 6:09pm On Nov 28, 2024 |
Here is the JavaScript code. Note that this code uses Node.js and several external libraries, including `whisper.js`, `sound-device`, `fs`, `path`, `bcryptjs`, and `js-yaml`. user_setup.js const whisper = require('whisper.js'); const soundDevice = require('sound-device'); const fs = require('fs'); const path = require('path'); const bcrypt = require('bcryptjs'); const yaml = require('js-yaml'); // Load model const model = whisper.loadModel('small'); const fsSampleRate = 44100; // Sample rate const seconds = 3; // Duration of recording async function userSetup() { console.log("Do you want to setup a new user? " ;const answerRecording = await recordAudio(); await writeAudioToFile(answerRecording, 'answer.mp3'); const answerTranscription = await transcribeAudio('answer.mp3'); const answer = answerTranscription.toLowerCase().replace(/[^a-z]/g, ''); console.log(answer); if (answer === 'yes') { console.log("Please set your username" ;const usernameList = await usernameSetup(); console.log(usernameList); if (compareElements(usernameList)) { console.log("Your username has been set" ;console.log("Please set up your password" ;const passList = await passSetup(); console.log(passList); if (compareElements(passList)) { console.log("Your password has been set" ;await database(usernameList[0], passList[0]); console.log("User setup is now completed" ;} else { console.log("System now exiting due to mismatching passphrases." ;process.exit(); } } else { console.log("System now exiting due to mismatching usernames." ;process.exit(); } } else if (answer === 'no') { console.log("Have a great day!" ;process.exit(); } else { console.log("Invalid response." ;process.exit(); } } // Helper functions async function recordAudio() { return new Promise((resolve, reject) => { const stream = soundDevice.open({ channels: 2, sampleRate: fsSampleRate, device: null }); const chunks = []; stream.on('data', (chunk) => { chunks.push(chunk); }); stream.on('end', () => { const audioBuffer = Buffer.concat(chunks); resolve(audioBuffer); }); stream.start(); setTimeout(() => { stream.stop(); }, seconds * 1000); }); } async function writeAudioToFile(audioBuffer, filename) { return new Promise((resolve, reject) => { fs.writeFile(filename, audioBuffer, (err) => { if (err) { reject(err); } else { resolve(); } }); }); } async function transcribeAudio(filename) { return new Promise((resolve, reject) => { model.transcribe(filename, (err, result) => { if (err) { reject(err); } else { resolve(result); } }); }); } async function usernameSetup() { // Implement username setup logic here return ['username']; } async function passSetup() { // Implement password setup logic here return ['password']; } async function database(username, password) { // Implement database logic here } function compareElements(list) { // Implement comparison logic here return true; } // Call userSetup function userSetup(); This code does the following: 1. Loads the Whisper model. 2. Defines an asynchronous `userSetup` function that: - Asks the user if they want to set up a new user. - Records the user's response using the `sound-device` library. - Transcribes the recorded audio using the Whisper model. - Checks the user's response and proceeds accordingly. - Sets up the username and password. - Saves the username and password to a database. 3. Defines several helper functions for recording audio, writing audio to files, transcribing audio, setting up usernames and passwords, and comparing elements. 4. Calls the `userSetup` function. |
| Re: Voice Authentication In Nodejs by Alphabyte3(op): 3:56pm On Nov 28, 2024*. Modified: 6:08pm On Nov 28, 2024 |
Here is the JavaScript code. Note that this code uses Node.js and several external libraries, including `whisper.js`, `sound-device`, `fs`, `path`, `bcryptjs`, and `js-yaml`. verification.js const whisper = require('whisper.js'); const soundDevice = require('sound-device'); const fs = require('fs'); const path = require('path'); const bcrypt = require('bcryptjs'); const yaml = require('js-yaml'); // Load model const model = whisper.loadModel('small'); const fsSampleRate = 44100; // Sample rate const seconds = 3; // Duration of recording async function verify() { const filePath = path.join(__dirname, 'output.yaml'); if (!fs.existsSync(filePath)) { console.log("There is no existed user in the database. Please create a user first." ;await userSetup(); } else { // Get username console.log("Please say your username " ;const usernameRecording = await recordAudio(); await writeAudioToFile(usernameRecording, 'username_input.mp3'); const usernameTranscription = await transcribeAudio('username_input.mp3'); const username = usernameTranscription.toLowerCase().replace(/\W+/g, ''); // Get passphrase console.log("Please say your passphrase " ;const passphraseRecording = await recordAudio(); await writeAudioToFile(passphraseRecording, 'passphrase_input.mp3'); const passphraseTranscription = await transcribeAudio('passphrase_input.mp3'); const pwd = passphraseTranscription.toLowerCase().replace(/\W+/g, ''); // Read YAML file const fileContent = fs.readFileSync(filePath, 'utf8'); const databaseConfig = yaml.safeLoad(fileContent); // Check password if (bcrypt.compareSync(pwd, databaseConfig[username])) { console.log('match'); return true; } else { console.log('no match'); return false; } } } // Helper functions async function recordAudio() { return new Promise((resolve, reject) => { const stream = soundDevice.open({ channels: 2, sampleRate: fsSampleRate, device: null }); const chunks = []; stream.on('data', (chunk) => { chunks.push(chunk); }); stream.on('end', () => { const audioBuffer = Buffer.concat(chunks); resolve(audioBuffer); }); stream.start(); setTimeout(() => { stream.stop(); }, seconds * 1000); }); } async function writeAudioToFile(audioBuffer, filename) { return new Promise((resolve, reject) => { fs.writeFile(filename, audioBuffer, (err) => { if (err) { reject(err); } else { resolve(); } }); }); } async function transcribeAudio(filename) { return new Promise((resolve, reject) => { model.transcribe(filename, (err, result) => { if (err) { reject(err); } else { resolve(result); } }); }); } async function userSetup() { // Implement user setup logic here } // Call verify function verify(); This code does the following: 1. Loads the Whisper model. 2. Defines an asynchronous `verify` function that: - Checks if the `output.yaml` file exists. - If not, logs a message and calls the `userSetup` function. - Records the username and passphrase using the `sound-device` library. - Transcribes the recorded audio using the Whisper model. - Reads the `output.yaml` file and checks if the transcribed username and passphrase match the stored credentials. - Logs a message indicating whether the credentials match or not. 3. Defines several helper functions for recording audio, writing audio to files, transcribing audio, and setting up users. 4. Calls the `verify` function. |
| Re: Voice Authentication In Nodejs by Alphabyte3(op): 4:13pm On Nov 28, 2024 |
In conclusion voice authentication is vulnerable to security breaches due to advancements in AI technology. AI can clone any voice, making it possible for hackers to mimic a person's voice and gain unauthorized access. This raises concerns about the reliability of voice authentication systems, highlighting the need for additional security measures to prevent voice spoofing attacks. |
| Re: Voice Authentication In Nodejs by Deicide: 8:54am On Nov 29, 2024 |
Using require in 2024 is wild o |
| Re: Voice Authentication In Nodejs by Alphabyte3(op): 12:43pm On Nov 29, 2024 |
Deicide:Require a package from npm or customs packages u use it in your backend in express or nodejs |
| Re: Voice Authentication In Nodejs by Alphabyte3(op): 12:57pm On Nov 29, 2024*. Modified: 1:45pm On Mar 22, 2025 |
Actually u need npm packages if you are connecting js library or framework using axios or fetch to other languages like using Java ,C# ,Go , Python and PHP. The backend and frontend is split into two while the hosting is done separately with different server ports. |
How Do You Handle Authentication In Your Projects? What's The Industry Standard? • How To Use Google Login In Nodejs • Facing Problem Integrating Paystack With Nodejs • 2 • 3 • 4
Devastating Effects Of Software Errors: A Wake-up Call • First Completed Project In 2026 • Are You A Software Developer? Enhance Your Skill Sets In Secure Coding
;