Skip to content

Commit 4188e02

Browse files
committed
impl: 优化密码表rw逻辑
1 parent 4c61d89 commit 4188e02

3 files changed

Lines changed: 25 additions & 27 deletions

File tree

cmd/wutils/zip/crack.go

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,26 +42,32 @@ func getHomePasswordFilePath() (string, error) {
4242
}
4343

4444
func CrackPassword(archivePath string) string {
45-
passwordFilePath, err := getHomePasswordFilePath()
46-
if err != nil {
47-
fmt.Printf("Error: %v\n", err)
48-
return ""
49-
}
45+
return CrackPasswordWithList(archivePath, nil)
46+
}
5047

51-
passwords, err := os.ReadFile(passwordFilePath)
52-
if err != nil {
53-
fmt.Printf("Error: failed to read password file: %v\n", err)
54-
return ""
55-
}
48+
func CrackPasswordWithList(archivePath string, passwords []string) string {
49+
if passwords == nil {
50+
passwordFilePath, err := getHomePasswordFilePath()
51+
if err != nil {
52+
fmt.Printf("Error: %v\n", err)
53+
return ""
54+
}
5655

57-
passwordList := parsePasswords(string(passwords))
58-
if len(passwordList) == 0 {
59-
fmt.Println("Error: no valid passwords found in dictionary")
60-
fmt.Printf("Please add passwords to: %s\n", passwordFilePath)
61-
return ""
56+
passwordData, err := os.ReadFile(passwordFilePath)
57+
if err != nil {
58+
fmt.Printf("Error: failed to read password file: %v\n", err)
59+
return ""
60+
}
61+
62+
passwords = parsePasswords(string(passwordData))
63+
if len(passwords) == 0 {
64+
fmt.Println("Error: no valid passwords found in dictionary")
65+
fmt.Printf("Please add passwords to: %s\n", passwordFilePath)
66+
return ""
67+
}
6268
}
6369

64-
result := crackWithWorkers(archivePath, passwordList, defaultMaxWorkers)
70+
result := crackWithWorkers(archivePath, passwords, defaultMaxWorkers)
6571
if result != "" {
6672
fmt.Printf("Password found: %s\n", result)
6773
} else {

cmd/wutils/zip/crack_test.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
11
package zip
22

33
import (
4-
"os"
5-
"path/filepath"
64
"testing"
75
)
86

97
func TestCrackPassword(t *testing.T) {
10-
localDictPath := "./password-dict.txt"
11-
os.WriteFile(localDictPath, []byte("test\nwrong\n123456"), 0644)
12-
defer os.Remove(localDictPath)
13-
14-
home, _ := os.UserHomeDir()
15-
homeDictPath := filepath.Join(home, ".config", "wutils", "password-dict.txt")
16-
os.WriteFile(homeDictPath, []byte("test\nwrong\n123456"), 0644)
17-
188
type args struct {
199
archivePath string
10+
passwords []string
2011
}
2112
tests := []struct {
2213
name string
@@ -27,13 +18,14 @@ func TestCrackPassword(t *testing.T) {
2718
name: "test",
2819
args: args{
2920
archivePath: "test/test.zip",
21+
passwords: []string{"test", "wrong", "123456"},
3022
},
3123
want: "test",
3224
},
3325
}
3426
for _, tt := range tests {
3527
t.Run(tt.name, func(t *testing.T) {
36-
if got := CrackPassword(tt.args.archivePath); got != tt.want {
28+
if got := CrackPasswordWithList(tt.args.archivePath, tt.args.passwords); got != tt.want {
3729
t.Errorf("CrackPassword() = %v, want %v", got, tt.want)
3830
}
3931
})

pkg/zip/testoutput/test.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)