Skip to content

Commit

Permalink
password: fix sequencer documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
jedib0t committed Apr 20, 2024
1 parent c012590 commit 40cf7bc
Showing 1 changed file with 7 additions and 15 deletions.
22 changes: 7 additions & 15 deletions password/sequencer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,23 @@ import (
"math/big"
"math/rand/v2"
"sync"
"time"
)

var (
biZero = big.NewInt(0)
biOne = big.NewInt(1)
)

// Sequencer is a deterministic Password Generator that generates all possible
// combinations of passwords for a Charset and defined number of characters in
// the password. It lets you move back and forth through the list of possible
// passwords, and involves no RNG.
type Sequencer interface {
// First moves to the first possible password and returns the same.
First() string
// Get returns the current password in the sequence.
Get() string
// GetN returns the password at location N, given 0 <= N < MaxPossibleWords
// GetN returns the value for N (location in list of possible passwords).
GetN() *big.Int
// GotoN overrides N.
GotoN(n *big.Int) (string, error)
Expand All @@ -37,8 +40,6 @@ type Sequencer interface {
PrevN(n *big.Int) string
// Reset cleans up state and moves to the first possible word.
Reset()
// SetSeed overrides the seed value for the RNG.
SetSeed(seed uint64)
// Stream sends all possible passwords in order to the given channel. If you
// want to limit output, pass in a *big.Int with the number of passwords you
// want to be generated and streamed.
Expand All @@ -65,7 +66,6 @@ type sequencer struct {
// interface.
func NewSequencer(rules ...Rule) (Sequencer, error) {
s := &sequencer{}
s.SetSeed(uint64(time.Now().UnixNano()))
for _, rule := range append(basicRules, rules...) {
rule(s)
}
Expand Down Expand Up @@ -110,15 +110,15 @@ func (s *sequencer) Get() string {
return s.get()
}

// GetN returns the password at location N, given 0 <= N <= MaxPossibleWords
// GetN returns the current location in the list of possible passwords.
func (s *sequencer) GetN() *big.Int {
s.mutex.Lock()
defer s.mutex.Unlock()

return new(big.Int).Set(s.n)
}

// GotoN overrides N.
// GotoN overrides the current location in the list of possible passwords.
func (s *sequencer) GotoN(n *big.Int) (string, error) {
s.mutex.Lock()
defer s.mutex.Unlock()
Expand Down Expand Up @@ -201,14 +201,6 @@ func (s *sequencer) Reset() {
s.First()
}

// SetSeed changes the seed value of the RNG.
func (s *sequencer) SetSeed(seed uint64) {
s.mutex.Lock()
defer s.mutex.Unlock()

s.rng = rand.New(rand.NewPCG(seed, seed+100))
}

// Stream sends all possible passwords in order to the given channel. If you
// want to limit output, pass in a *big.Int with the number of passwords you
// want to be generated and streamed.
Expand Down

0 comments on commit 40cf7bc

Please sign in to comment.