| | |
| | | <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> |
| | | <h3>Kryptographie</h3> |
| | | </section> |
| | | <section> |
| | | Mithilfe des nativen <code>crypto</code>-Moduls ist es möglich, moderne sichere kryptografische Verfahren zu verwenden: |
| | | <pre> |
| | | <code class="js" data-trim data-line-numbers> |
| | | const crypto = require("crypto"); |
| | | </code> |
| | | </pre> |
| | | Hierzu nutzt NodeJS selber die weit verbreitete OpenSSL-Bibliothek |
| | | </section> |
| | | <section> |
| | | Einen MD5-Hash erzeugen: |
| | | <pre> |
| | | <code class="js" data-trim data-line-numbers> |
| | | const crypto = require("crypto"); |
| | | |
| | | const hash = crypto.createHash("md5"); |
| | | const md5 = hash.update("abcdefg") |
| | | .digest(); // Buffer (default) |
| | | // .digest("hex") // Hexadezimal |
| | | // .digest("utf8") // Bytes forciert als Chars |
| | | |
| | | console.log(md5); |
| | | </code> |
| | | </pre> |
| | | </section> |
| | | <section> |
| | | <p>Aufgabe</p> |
| | | <p>Erzeuge jeweils den md5- und den sha256-Hash als Hex-Wert der beiden Texte in der Datei <code>text-examples.txt</code> und vergleiche jeweils ihren md5- und sha256-Hash miteinander</p> |
| | | </section> |
| | | <section> |
| | | <h3>Zippen</h3> |
| | | </section> |
| | | <section> |
| | | Mit dem nativen Modul <code>zlib</code> ist es möglich, Kompression in seinen Anwendungen zu nutzen. |
| | | <pre> |
| | | <code class="js" data-trim data-line-numbers> |
| | | const zlib = require("zlib"); |
| | | |
| | | // Komprimieren |
| | | zlib.deflate("abc" /* buffer | string */, (err, compressed) => {}); |
| | | |
| | | // Dekomprimieren |
| | | zlib.inflate(compressed /* buffer */, (err, decompressed) => {}); |
| | | |
| | | // Als Stream |
| | | const zipper = zlib.createDeflate(); |
| | | const unzipper = zlib.createInflate(); |
| | | |
| | | source.pipe(zipper).pipe(target); |
| | | source.pipe(unzipper).pipe(target); |
| | | </code> |
| | | </pre> |
| | | </section> |
| | | <section> |
| | | <p>Aufgabe</p> |
| | | <p>Nutze die einfachen Varianten <code>zlib.deflate</code> und <code>zlib.inflate</code>, um einen beliebigen Text zu komprimieren und anschließend wieder zu dekomprimieren</p> |
| | | </section> |
| | | <section> |
| | | <p>Aufgabe</p> |
| | | <p>Nutze die Stream-Varianten <code>zlib.createDeflate</code> und <code>zlib.createInflate</code>, um jeweils ein getrenntes Programm für die Kompression und Dekompression mit <code>process.stdin</code> und <code>process.stdout</code> zu machen, sodass in der Konsole folgende Nutzung möglich ist:</p> |
| | | <pre> |
| | | <code class="bash" data-trim data-line-numbers> |
| | | echo "abc" | node compress.js | node decompress.js |
| | | cat file.txt | node compress.js > file.zipped |
| | | cat file.zipped | node decompress.js > file.txt |
| | | </code> |
| | | </pre> |
| | | </section> |
| | | </section> |
| | | </div> |
| | | </div> |