angch.venice.1.12.34.source-code.aes-speed.venice Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of venice Show documentation
Show all versions of venice Show documentation
Venice, a sandboxed Lisp implemented in Java.
The newest version!
;;;; __ __ _
;;;; \ \ / /__ _ __ (_) ___ ___
;;;; \ \/ / _ \ '_ \| |/ __/ _ \
;;;; \ / __/ | | | | (_| __/
;;;; \/ \___|_| |_|_|\___\___|
;;;;
;;;;
;;;; Copyright 2017-2024 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 AES-256 / ChaCha20 encryption/decryption speed
;; MacBookAir M2, Java 8 (Zulu), BouncyCastle 1.77
;; --------------------------------------------------------------------
;; 2KB 20KB 200KB 2MB 20MB 200MB
;; --------------------------------------------------------------------
;; Encrypt AES-256 CBC: 85ms 65ms 66ms 74ms 172ms 1165ms
;; Decrypt AES-256 CBC: 67ms 67ms 65ms 76ms 162ms 1053ms
;; Encrypt AES-256 GCM: 64ms 65ms 70ms 96ms 364ms 3170ms
;; Decrypt AES-256 GCM: 66ms 65ms 67ms 94ms 363ms 3215ms
;; Encrypt AES-256 ZIP: 11ms 5ms 10ms 60ms 565ms 5681ms
;; Decrypt AES-256 ZIP: 7ms 5ms 6ms 24ms 204ms 2045ms
;; Encrypt ChaCha20: - - - - - -
;; Decrypt ChaCha20: - - - - - -
;; Encrypt ChaCha20-BC: 75ms 63ms 66ms 71ms 127ms 701ms
;; Decrypt ChaCha20-BC: 66ms 65ms 65ms 71ms 127ms 704ms
;; --------------------------------------------------------------------
;; MacBookAir M2, Java 17 (Zulu), BouncyCastle 1.77
;; --------------------------------------------------------------------
;; 2KB 20KB 200KB 2MB 20MB 200MB
;; --------------------------------------------------------------------
;; Encrypt AES-256 CBC: 96ms 73ms 73ms 84ms 193ms 1337ms
;; Decrypt AES-256 CBC: 75ms 72ms 74ms 85ms 195ms 1562ms
;; Encrypt AES-256 GCM: 73ms 72ms 75ms 103ms 388ms 3593ms
;; Decrypt AES-256 GCM: 75ms 73ms 76ms 103ms 392ms 3283ms
;; Encrypt AES-256 ZIP: 7ms 5ms 9ms 60ms 600ms 5900ms
;; Decrypt AES-256 ZIP: 7ms 4ms 7ms 26ms 240ms 2311ms
;; Encrypt ChaCha20: 83ms 72ms 73ms 77ms 119ms 566ms
;; Decrypt ChaCha20: 73ms 72ms 73ms 76ms 118ms 527ms
;; Encrypt ChaCha20-BC: 74ms 73ms 73ms 87ms 160ms 949ms
;; Decrypt ChaCha20-BC: 74ms 73ms 74ms 85ms 160ms 931ms
;; --------------------------------------------------------------------
(do
(load-module :zipvault)
(load-module :crypt)
(load-module :timing ['timing :as 't])
(load-module :matrix)
(load-module :ascii-table)
(defonce passwd "j87vhfrtxvzrzver445dffg")
(defonce test-dir (io/file (io/user-home-dir) "Desktop/venice/tmp"))
(defn format-size [size-kb]
(if (< size-kb 1000) (str size-kb "KB") (str (/ size-kb 1000) "MB")))
(defn identical [file1 file2]
(= (io/slurp file1 :binary true) (io/slurp file2 :binary true)))
(defn encrypt [algo passphrase file-in file-out]
(crypt/encrypt-file algo passphrase file-in file-out))
(defn decrypt [algo passphrase file-in file-out]
(crypt/decrypt-file algo passphrase file-in file-out))
(defn encrypt-zip [zip passphrase filename file ]
(zipvault/zip zip passphrase filename (io/slurp file :binary true)))
(defn decrypt-zip [zip passphrase filename destpath]
(zipvault/extract-file zip passphrase filename destpath))
(defn run [size-kb]
(let [size (* size-kb 1024)
data (bytebuf-allocate-random size)
data-file (io/file test-dir "test.data")
aes-cbc-file-enc (io/file test-dir "test.data.aes-cbc.enc")
aes-cbc-file-dec (io/file test-dir "test.data.aes-cbc.dec")
aes-gcm-file-enc (io/file test-dir "test.data.aes-gcm.enc")
aes-gcm-file-dec (io/file test-dir "test.data.aes-gcm.dec")
cha-file-enc (io/file test-dir "test.data.cha.enc")
cha-file-dec (io/file test-dir "test.data.cha.dec")
cha-bc-file-enc (io/file test-dir "test.data.cha-bc.enc")
cha-bc-file-dec (io/file test-dir "test.data.cha-bc.dec")
aes-file-zip (io/file test-dir "test.data.zip")
aes-file-unzip (io/file test-dir "test.data.unzip")]
(println "Testing file:" data-file (str size-kb "KB"))
;; create the test data file (a buffer with random bytes)
(io/spit data-file data)
(io/slurp data-file :binary true) ;; warm up os file read
(let [timings [ (t/elapsed #(encrypt "AES256-CBC" passwd
data-file
aes-cbc-file-enc))
(t/elapsed #(decrypt "AES256-CBC" passwd
aes-cbc-file-enc
aes-cbc-file-dec))
(t/elapsed #(encrypt "AES256-GCM" passwd
data-file
aes-gcm-file-enc))
(t/elapsed #(decrypt "AES256-GCM" passwd
aes-gcm-file-enc
aes-gcm-file-dec))
(if (crypt/encrypt-file-supported? "ChaCha20")
[ (t/elapsed #(encrypt "ChaCha20" passwd
data-file
cha-file-enc))
(t/elapsed #(decrypt "ChaCha20" passwd
cha-file-enc
cha-file-dec))]
["-" "-"])
(if (crypt/encrypt-file-supported? "ChaCha20-BC")
[ (t/elapsed #(encrypt "ChaCha20-BC" passwd
data-file
cha-bc-file-enc))
(t/elapsed #(decrypt "ChaCha20-BC" passwd
cha-bc-file-enc
cha-bc-file-dec))]
["-" "-"])
(t/elapsed #(encrypt-zip aes-file-zip
passwd
(io/file-name aes-file-unzip)
data-file))
(t/elapsed #(decrypt-zip aes-file-zip
passwd
(io/file-name aes-file-unzip)
(io/file-parent aes-file-unzip)))
]]
(println "Decrypt verify AES CBC: "
(if (identical data-file aes-cbc-file-dec) "OK" "FAIL"))
(println "Decrypt verify AES GCM: "
(if (identical data-file aes-gcm-file-dec) "OK" "FAIL"))
(when (crypt/encrypt-file-supported? "ChaCha20")
(println "Decrypt verify ChaCha20: "
(if (identical data-file cha-file-dec) "OK" "FAIL")))
(when (crypt/encrypt-file-supported? "ChaCha20-BC")
(println "Decrypt verify ChaCha20-BC: "
(if (identical data-file cha-bc-file-dec) "OK" "FAIL")))
(println "Decrypt verify ZIP: "
(if (identical data-file aes-file-unzip) "OK" "FAIL"))
(flatten timings))))
(defn sections []
[ "Encrypt AES-256 CBC"
"Decrypt AES-256 CBC"
"Encrypt AES-256 GCM"
"Decrypt AES-256 GCM"
"Encrypt ChaCha20"
"Decrypt ChaCha20"
"Encrypt ChaCha20-BC"
"Decrypt ChaCha20-BC"
"Encrypt AES-256 ZIP"
"Decrypt AES-256 ZIP"] )
(defn run-sample []
(when-not (io/exists-dir? test-dir)
(throw (ex :VncException (str "The dir " test-dir " does not exist!)"))))
(let [samples [2 20 200 2000 20000 200000]
sections (sections)
columns (cons {:header {:text "" } :width 19}
(map #(hash-map :header {:text (format-size %)
:align :right}
:body {:align :right}
:width 6)
samples))]
(as-> (map #(run %) samples) data
(matrix/vector2d data)
(matrix/transpose data)
(matrix/add-column-at-start data sections)
(ascii-table/print columns data :bold-no-data 1)))))