Skip to content

Commit

Permalink
added an example script comparing MD5, SHA-1, SHA-256 hashing
Browse files Browse the repository at this point in the history
performance
  • Loading branch information
juerg committed Dec 8, 2023
1 parent 633274d commit 6114f17
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 6 deletions.
2 changes: 1 addition & 1 deletion ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ All notable changes to this project will be documented in this file.
- bytebuf allocation with a fill value
- an example script comparing AES-256 CBC, AES-256 GCM, and encrypted ZIP
encryption and decryption performance

- an example script comparing MD5, SHA-1, SHA-256 hashing performance


## [1.10.54] - 2023-12-07
Expand Down
9 changes: 4 additions & 5 deletions doc/examples/scripts/aes-speed.venice
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@
(defn printel [title elapsed]
(printf "%-17s %4dms%n" title elapsed))

(defn filesize [file]
(str (long (/ (io/file-size file) 1024 1024)) " MB"))

(defn encrypt-aes [file-in file-out passphrase]
(let [encryptor (crypt/encrypt "AES256" passphrase)]
(->> (io/slurp file-in :binary true)
Expand Down Expand Up @@ -81,6 +78,7 @@

(defn run [size-kb]
(let [size (* size-kb 1024)
data (bytebuf-allocate-random size)
dir (io/file (io/user-home-dir) "Desktop/aes-test")
data-file (io/file dir "test.data")
aes-file-cbc-enc (io/file dir "test.data.aes-cbc.enc")
Expand All @@ -96,7 +94,8 @@
(println " size:" size)

;; create the test data file (a buffer with random bytes)
(io/spit data-file (bytebuf-allocate-random size))
(io/spit data-file data)
(io/slurp file :binary true) ;; warm up os file read
(println " : created")
(println)

Expand Down Expand Up @@ -142,5 +141,5 @@
(if (identical data-file aes-file-unzip) "OK" "FAIL"))))

(defn run-sample []
(docoll #(do (run %) (println)(println))
(docoll #(do (run %) (println) (println))
[2 20 200 2000 20000 200000])))
86 changes: 86 additions & 0 deletions doc/examples/scripts/hash-speed.venice
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
;;;; __ __ _
;;;; \ \ / /__ _ __ (_) ___ ___
;;;; \ \/ / _ \ '_ \| |/ __/ _ \
;;;; \ / __/ | | | | (_| __/
;;;; \/ \___|_| |_|_|\___\___|
;;;;
;;;;
;;;; Copyright 2017-2023 Venice
;;;;
;;;; Licensed under the Apache License, Version 2.0 (the "License");
;;;; you may not use this file except in compliance with the License.
;;;; You may obtain a copy of the License at
;;;;
;;;; http://www.apache.org/licenses/LICENSE-2.0
;;;;
;;;; Unless required by applicable law or agreed to in writing, software
;;;; distributed under the License is distributed on an "AS IS" BASIS,
;;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;;;; See the License for the specific language governing permissions and
;;;; limitations under the License.

;;;; Compares hashing speed

;; MacBookAir M2 2022
;; --------------------------------------------------------------------
;; 2KB 20KB 200KB 2MB 20MB 200MB
;; --------------------------------------------------------------------
;; Hash MD5 (file): 1ms 1ms 1ms 6ms 56ms 547ms
;; Hash SHA-1 (file): 0ms 1ms 2ms 7ms 65ms 685ms
;; Hash SHA-256 (file): 0ms 0ms 2ms 8ms 77ms 764ms
;;
;; Hash MD5 (memory): 0ms 1ms 1ms 5ms 54ms 535ms
;; Hash SHA-1 (memory): 0ms 1ms 1ms 7ms 64ms 642ms
;; Hash SHA-256 (memory): 1ms 0ms 1ms 8ms 76ms 749ms
;; --------------------------------------------------------------------



(do
(load-module :crypt)
(load-module :timing)


(defonce passwd "j87vhfrtxvzrzver445dffg")
(defonce salt "12647458820938745388281")


(defn identical [file1 file2]
(= (io/slurp file1 :binary true)
(io/slurp file2 :binary true)))

(defn printel [title elapsed]
(printf "%-20s %4dms%n" title elapsed))

(defn run [size-kb]
(let [size (* size-kb 1024)
data (bytebuf-allocate-random size)
dir (io/file (io/user-home-dir) "Desktop/aes-test")
file (io/file dir "test.data")]
(when-not (io/exists-dir? dir)
(throw (ex :VncException (str "The dir " dir " doesnot exist!)"))))

(println "Testing file:" file (str size-kb "KB"))
(println " size:" size)

;; create the test data file (a buffer with random bytes)
(io/spit file data)
(io/slurp file :binary true) ;; warm up os file read
(println " : created")
(println)

;; file based
(docoll (fn [algo]
(->> (timing/elapsed #(crypt/hash-file file salt algo))
(printel (str "Hash " algo " (file):"))))
["MD5" "SHA-1" "SHA-256"])

;; memory based
(docoll (fn [algo]
(->> (timing/elapsed #(crypt/hash-file data salt algo))
(printel (str "Hash " algo " (mem):"))))
["MD5" "SHA-1" "SHA-256"])))

(defn run-sample []
(docoll #(do (run %) (println) (println))
[2 20 200 2000 20000 200000])))

0 comments on commit 6114f17

Please sign in to comment.