From 8661e4358cd8a40e94c39f40b6147a5f37a7c7d3 Mon Sep 17 00:00:00 2001 From: Sascha Schulz <sschulz@dh-software.de> Date: Fr, 26 Jul 2024 09:54:43 +0200 Subject: [PATCH] Merge branch 'draft' --- index.html | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 117 insertions(+), 0 deletions(-) diff --git a/index.html b/index.html index a80829f..91a7015 100644 --- a/index.html +++ b/index.html @@ -4122,6 +4122,123 @@ </code> </pre> </section> + <section> + <h3>Buffer</h3> + <p>Array-artige Struktur für rohe Bytes</p> + <pre> + <code class="js" data-trim data-line-numbers> + const b1 = Buffer.from("abc"); // bevorzugt + const b2 = Buffer.alloc(100, 0); // Länge mit initialen Daten + const b3 = Buffer.allocUnsafe(100); // Länge ohne initiale Daten + const b4 = new Buffer("abc"); // offiziell nicht empfohlen + + console.log(b1); // => <Buffer 61 62 63> + console.log(b1.toString("hex")); // => "616263" + </code> + </pre> + </section> + <section> + <p>Mit Buffern arbeiten:</p> + <pre> + <code class="js" data-trim data-line-numbers> + const a = Buffer.from("a") + Buffer.from("b"); + const b = Buffer.concat([Buffer.from("a"), Buffer.from([98])]); + const c = b.toString() + + console.log(a); // => ab + console.log(b); // => <Buffer 61 62> + console.log(c); // => ab + + for (const byte of a) { + console.log(byte); // => 97 98 + } + </code> + </pre> + </section> + <section> + <p>Aufgabe</p> + <p>Implementiere eine XOR-Verschlüsselung (<code>^</code>-Operator), sodass jedes Byte einer Original-Nachricht der Reihe nach mit dem entsprechenden Byte eines Schlüsselwortes verschlüsselt wird und gebe die verschlüsselte Nachricht als Text aus.</p> + </section> + <section> + <p>Aufgabe</p> + <p>Implementiere zur vorherigen Verschlüsselung eine Entschlüsselung.</p> + </section> + <section> + <p>Lösung</p> + <pre> + <code class="js" data-trim data-line-numbers> + const word = Buffer.from("abcdef"); + const key = Buffer.from("xyz"); + const encrypted = Buffer.alloc(word.length, 0); + + // encrypt + for (let i = 0; i < word.length; i++) { + encrypted[i] = word[i] ^ key[i % key.length]; + } + + // decrypt + for (let i = 0; i < word.length; i++) { + word[i] = encrypted[i] ^ key[i % key.length]; + } + + console.log(word.toString()); // => "abcdef" + </code> + </pre> + </section> + <section> + <h3>Streams</h3> + <p>Konstanter Fluss von (meist) großen Datenmengen in kleinen Paketen</p> + </section> + <section> + <p>Gängige Typen von Streams</p> + <ul> + <li>Standard In / Out</li> + <li>HTTP-Requests / -Responses</li> + <li>TCP-Sockets</li> + <li>Lese- und Schreib-Streams auf Dateien</li> + <li>Große Datenbank-Ergebnisse mittels Cursor</li> + </ul> + </section> + <section> + <pre> + <code class="js" data-trim data-line-numbers> + const readableStream = fs.createReadStream("./source.txt"); + const writableStream = fs.createWriteStream("./target.txt"); + + readableStream.on("data", (data /* Buffer */) => { + // process data here + writableStream.write(data) + }); + + readableStream.on("end", (data) => { + // stream has finished, e.g. EOF (end of file) + }); + + // alternatively pipe content + readableStream.pipe(writableStream); + </code> + </pre> + </section> + <section> + <p>Aufgabe</p> + <p>Lese eine beliebige Datei als Readable Stream, verschlüssele den Inhalt mit der XOR-Methode und schreibe die verschlüsselten Bytes in eine Zieldatei</p> + </section> + <section> + <p>Aufgabe</p> + <p>Ändere das Programm, sodass auf Konsolenebene ein Pipe (|) ermöglicht wird:</p> + <pre> + <code class="bash" data-trim data-line-numbers> + echo "Hello World!" | node index.js + </code> + </pre> + <p>Nutze dafür die implizit vorhandenen Streams von Standard In / Out:</p> + <pre> + <code class="js" data-trim data-line-numbers> + process.stdin; // Readable Stream + process.stdout; // Writable Stream + </code> + </pre> + </section> </section> </div> </div> -- Gitblit v1.9.3