Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions samoina/crypto-module.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { createServer } from 'node:http';
import { createHash } from 'node:crypto';
import colors from 'colors';

const port = 3000;

const server = createServer((req, res) => {
if (req.method === 'POST' && req.url === '/password') {
let data = '';

//data sent to the server arrives in chunks and .on()is a method that allows us to listen for data being sent to the server. all of the data is stored in the data variable
req.on('data', (chunk) => {
data += chunk;
});

//when the response is complete, get the PW, validate it nd hash it
req.on('end', () => {
const userPassword = JSON.parse(data).password;

if (!userPassword) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ message: 'Password is required' }));
return;
}

if (userPassword.length < 5) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(
JSON.stringify({ message: 'Password must be at least 5 characters' })
);
return;
}

//Hashing the user password
//1. createHash uses the sha256 algorithm to hash the password others are like 'sha1', 'md5', etc
//2. update() takes the password and updates it with the sha256 algorithm
//3. digest() returns the hashed password specifying that it should be in hex
const hash = createHash('sha256').update(userPassword).digest('hex');

res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ message: 'Password is valid.', hash }));
});
} else {
res.writeHead(404, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ message: 'Not found' }));
}
});

server.listen(port, '127.0.0.1', () => {
console.log(`Server is running on port ${port}`.yellow);
});
23 changes: 23 additions & 0 deletions samoina/notes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Mon 09 Dec task

Use any of Node's inbuilt modules and use it in your own file

## What are some of node's inbuilt modules?

Node.js has several inbuilt modules,including;

- os, fs, path, http

we already used the http module in the first two sessions of 'How-To-Node'

- os provides information about the operating system.

### Crypto Module

- crypto module performs cfryptographic operations eg hashing data, encryption and decryption and creating random numbers

#### Hashing

This is a method that changes plain text to encrypted (cipher) text. It is important to note that this is a one way function, so it is not possible to get the original text from the hashed value, but it is possible to regenerate it with the original text. This is what makes it ideal for managing passwords and login credentials.(this is different from encryption and decryption)

When coming up with the code for the crypto password usage, I read that Node.js handles HTTP requests and responses as streams, which are divided into smaller chunks to make processing data faster and more memory efficient.
20 changes: 20 additions & 0 deletions samoina/os-module.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { createServer } from 'node:http';
import os from 'node:os';
import colors from 'colors';

const port = 3000;

const server = createServer((req, res) => {
console.log('Request received');
res.writeHead(200, {
'Content-type': 'text/plain',
});
res.end(`Hello there, your operating system is ${os.platform()}`);
});

server.listen(port, '127.0.0.1', () => {
console.log(`Server is running on port ${port}`.yellow);
console.log(`Hello there, your operating system is ${os.platform()}`);
});