Skip to content

Commit

Permalink
Improve entropy calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
hermo committed May 23, 2024
1 parent 1b207a3 commit ed47f6e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
32 changes: 23 additions & 9 deletions entropy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,56 @@ package main
import (
"fmt"
"math"
"unicode"
)

// bruteForceEntropy calculates the entropy of a password based on the number of
// characters and character classes used
func bruteforceEntropy(password string) float64 {
func bruteForceEntropy(password string) float64 {
// deduce character classes used from password. Classes include lowercase,
// uppercase, digits, and symbols
const (
lowercaseSize = 26
uppercaseSize = 26
digitSize = 10
symbolSize = 32 // !"#¤%&/()=?@£$€{[]}\`|<>'*-_^~§½
)

lowercase := false
uppercase := false
digits := false
symbols := false

for _, c := range password {
switch {
case c >= 'a' && c <= 'z':
case unicode.IsLower(c):
lowercase = true
case c >= 'A' && c <= 'Z':
case unicode.IsUpper(c):
uppercase = true
case c >= '0' && c <= '9':
case unicode.IsDigit(c):
digits = true
default:
symbols = true
}
}
// add the number of characters in each class used

// Add the number of characters in each class used
characters := 0
if lowercase {
characters += 26
characters += lowercaseSize
}
if uppercase {
characters += 26
characters += uppercaseSize
}
if digits {
characters += 10
characters += digitSize
}
if symbols {
characters += 10
characters += symbolSize
}

if characters == 0 {
return 0 // Handling edge case where the password is empty
}

return float64(len(password)) * math.Log2(float64(characters))
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func run() error {
fmt.Println(passphrase)

if settings.ShowInfo {
bruteEnt := bruteforceEntropy(passphrase)
bruteEnt := bruteForceEntropy(passphrase)
wordlistEnt := wordlistEntropy(passphrase, '-', len(words))
fmt.Fprintln(os.Stderr, "Entropy and estimated time to crack using a fast GPU-based attack (20 MH/s, one or more RTX 4090):")
fmt.Fprintf(os.Stderr, "* Brute-force: %5.1f bits (%s)\n", bruteEnt, estimateTimeToCrack(bruteEnt))
Expand Down

0 comments on commit ed47f6e

Please sign in to comment.