Skip to content

Commit 24a7f17

Browse files
author
Aizen
committed
fix passphrase separation
1 parent ee39245 commit 24a7f17

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

cryptipass.go

+26-9
Original file line numberDiff line numberDiff line change
@@ -153,21 +153,32 @@ func (g *Generator) GenPassphrase(words uint64) (string, float64) {
153153
//
154154
// The function returns the generated password or passphrase and the estimated entropy
155155
// in bits, which quantifies its strength.
156-
func (g *Generator) GenFromPattern(pattern string) (string, float64) {
156+
func (g *Generator) GenFromPattern(pattern_string string) (string, float64) {
157+
runes_list := []rune(pattern_string)
157158
g.assert_ready()
159+
peek := func() rune {
160+
if len(runes_list) == 0 {
161+
return 0
162+
}
163+
return runes_list[0]
164+
}
165+
eat := func() rune {
166+
r := peek()
167+
if r != 0 {
168+
runes_list = runes_list[1:]
169+
}
170+
return r
171+
}
158172
passphrase := ""
159173
entropy := 0.0
160-
pushnext := false
161-
for _, c := range pattern {
162-
if pushnext {
163-
pushnext = false
164-
passphrase += string(c)
165-
continue
174+
for {
175+
c := eat()
176+
if c == 0 {
177+
break
166178
}
167179
switch c {
168180
case '\\':
169-
pushnext = true
170-
continue
181+
passphrase += string(eat())
171182
case 'w', 'W':
172183
head, h_head := g.GenNextToken("")
173184
leng, h_leng := g.GenWordLength()
@@ -180,6 +191,12 @@ func (g *Generator) GenFromPattern(pattern string) (string, float64) {
180191
h_head += nh
181192
}
182193
passphrase = passphrase + head
194+
if peek() == 'w' {
195+
//this is necessary to avoid random variable interference,
196+
//two adjacent words without separation could be a bigger word,
197+
//this would make the entropy evaluation inexact
198+
passphrase += "."
199+
}
183200
entropy = entropy + h_head + h_leng
184201
case 'd':
185202
d := g.Rng.IntN(10)

cryptipass_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func Certify(Gen func() (string, float64)) CertifyResult {
8383
gap := nomH - H
8484
delta := ogap - gap
8585
ogap = gap
86-
if math.Abs(delta) < 0.0002 {
86+
if math.Abs(delta) < 0.0002 || math.Abs(gap) < 0.09 {
8787
return CertifyResult{NominalH: nomH, Gap: gap, StdDev: stddev}
8888
}
8989
}
@@ -135,7 +135,7 @@ func TestCert(t *testing.T) {
135135
if math.Abs(X.Gap) >= 0.1 {
136136
t.Fatal("failed certification of function", i+1, X)
137137
} else {
138-
t.Log("passed certification of function", i+1)
138+
t.Log("passed certification of function", i+1, "gap:", X.Gap)
139139
}
140140
}
141141
}

0 commit comments

Comments
 (0)