Last active
January 27, 2022 13:56
-
-
Save darkarnium/56c078c30bb359d8e013e8f56af80c3d to your computer and use it in GitHub Desktop.
Go vs Python - SHA1 and MD5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package hasher | |
import ( | |
"crypto/md5" | |
"crypto/sha1" | |
"encoding/hex" | |
"io" | |
"os" | |
) | |
func HashSHA1(chunk int) string { | |
file, err := os.Open("nexus_latest.tar") | |
if err != nil { | |
panic(err) | |
} | |
defer file.Close() | |
hash := sha1.New() | |
buffer := make([]byte, chunk) | |
for { | |
n, err := file.Read(buffer) | |
hash.Write(buffer[0:n]) | |
if err == io.EOF { | |
break | |
} | |
} | |
return hex.EncodeToString(hash.Sum(nil)) | |
} | |
func HashMD5(chunk int) string { | |
file, err := os.Open("nexus_latest.tar") | |
if err != nil { | |
panic(err) | |
} | |
defer file.Close() | |
hash := md5.New() | |
buffer := make([]byte, chunk) | |
for { | |
n, err := file.Read(buffer) | |
hash.Write(buffer[0:n]) | |
if err == io.EOF { | |
break | |
} | |
} | |
return hex.EncodeToString(hash.Sum(nil)) | |
} | |
func HashSHA1Copy() string { | |
file, err := os.Open("nexus_latest.tar") | |
if err != nil { | |
panic(err) | |
} | |
defer file.Close() | |
hash := sha1.New() | |
io.Copy(hash, file) | |
return hex.EncodeToString(hash.Sum(nil)) | |
} | |
func HashMD5Copy() string { | |
file, err := os.Open("nexus_latest.tar") | |
if err != nil { | |
panic(err) | |
} | |
defer file.Close() | |
hash := md5.New() | |
io.Copy(hash, file) | |
return hex.EncodeToString(hash.Sum(nil)) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Compare hash rates of MD5 and SHA1 over N rounds and X chunk size.""" | |
import sys | |
import timeit | |
import hashlib | |
def hash(sz=10240, func=hashlib.sha1): | |
h = func() | |
with open('nexus_latest.tar', "rb") as fin: | |
while chunk := fin.read(sz): | |
h.update(chunk) | |
return h.hexdigest() | |
def benchmark_md5_chunk_8(rounds: int): | |
md5 = timeit.Timer(lambda: hash(sz=8 * 1024, func=hashlib.md5)).timeit(number = rounds) | |
print(f"ok\thasher\t{md5}s") | |
def benchmark_sha1_chunk_8(rounds: int): | |
md5 = timeit.Timer(lambda: hash(sz=8 * 1024, func=hashlib.sha1)).timeit(number = rounds) | |
print(f"ok\thasher\t{md5}s") | |
if __name__ == "__main__": | |
# This is amazingly gross, but we're a benchmark. | |
if len(sys.argv) < 3: | |
print("Usage: hasher.py <case> <rounds>") | |
sys.exit(0) | |
case = getattr(sys.modules[__name__], sys.argv[1]) | |
count = int(sys.argv[2]) | |
case(count) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package hasher | |
import ( | |
"testing" | |
) | |
var result string | |
func BenchmarkMD5Chunk8(b *testing.B) { | |
var r string | |
for i := 0; i < b.N; i++ { | |
r = HashMD5(8 * 1024) | |
} | |
result = r | |
} | |
func BenchmarkSHA1Chunk8(b *testing.B) { | |
var r string | |
for i := 0; i < b.N; i++ { | |
r = HashSHA1(8 * 1024) | |
} | |
result = r | |
} | |
func BenchmarkMD5Copy(b *testing.B) { | |
var r string | |
for i := 0; i < b.N; i++ { | |
r = HashMD5Copy() | |
} | |
result = r | |
} | |
func BenchmarkSHA1Copy(b *testing.B) { | |
var r string | |
for i := 0; i < b.N; i++ { | |
r = HashSHA1Copy() | |
} | |
result = r | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Full output requested when run with
-benchtime 10x
and-count 10
:MD5;
SHA1: