From 179755d7782c2ef18fb19b30dda0a538bfb5823e Mon Sep 17 00:00:00 2001 From: Sascha Schulz <sschulz@dh-software.de> Date: Mo, 16 Sep 2024 15:46:32 +0200 Subject: [PATCH] add crypto and zlib chapters --- index.html | 71 +++++++++++++++++++++++++++++++++++ 1 files changed, 71 insertions(+), 0 deletions(-) diff --git a/index.html b/index.html index 91a7015..7c8aa23 100644 --- a/index.html +++ b/index.html @@ -4239,6 +4239,77 @@ </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> -- Gitblit v1.9.3