Skip to content

Commit

Permalink
fix passphrase separation
Browse files Browse the repository at this point in the history
  • Loading branch information
Aizen committed Oct 7, 2024
1 parent ee39245 commit 24a7f17
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
35 changes: 26 additions & 9 deletions cryptipass.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,21 +153,32 @@ func (g *Generator) GenPassphrase(words uint64) (string, float64) {
//
// The function returns the generated password or passphrase and the estimated entropy
// in bits, which quantifies its strength.
func (g *Generator) GenFromPattern(pattern string) (string, float64) {
func (g *Generator) GenFromPattern(pattern_string string) (string, float64) {
runes_list := []rune(pattern_string)
g.assert_ready()
peek := func() rune {
if len(runes_list) == 0 {
return 0
}
return runes_list[0]
}
eat := func() rune {
r := peek()
if r != 0 {
runes_list = runes_list[1:]
}
return r
}
passphrase := ""
entropy := 0.0
pushnext := false
for _, c := range pattern {
if pushnext {
pushnext = false
passphrase += string(c)
continue
for {
c := eat()
if c == 0 {
break
}
switch c {
case '\\':
pushnext = true
continue
passphrase += string(eat())
case 'w', 'W':
head, h_head := g.GenNextToken("")
leng, h_leng := g.GenWordLength()
Expand All @@ -180,6 +191,12 @@ func (g *Generator) GenFromPattern(pattern string) (string, float64) {
h_head += nh
}
passphrase = passphrase + head
if peek() == 'w' {
//this is necessary to avoid random variable interference,
//two adjacent words without separation could be a bigger word,
//this would make the entropy evaluation inexact
passphrase += "."
}
entropy = entropy + h_head + h_leng
case 'd':
d := g.Rng.IntN(10)
Expand Down
4 changes: 2 additions & 2 deletions cryptipass_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func Certify(Gen func() (string, float64)) CertifyResult {
gap := nomH - H
delta := ogap - gap
ogap = gap
if math.Abs(delta) < 0.0002 {
if math.Abs(delta) < 0.0002 || math.Abs(gap) < 0.09 {
return CertifyResult{NominalH: nomH, Gap: gap, StdDev: stddev}
}
}
Expand Down Expand Up @@ -135,7 +135,7 @@ func TestCert(t *testing.T) {
if math.Abs(X.Gap) >= 0.1 {
t.Fatal("failed certification of function", i+1, X)
} else {
t.Log("passed certification of function", i+1)
t.Log("passed certification of function", i+1, "gap:", X.Gap)
}
}
}
Expand Down

0 comments on commit 24a7f17

Please sign in to comment.