Skip to content

Commit c643147

Browse files
author
Nurio Fernández
committed
Renamed all functions to UpperCamelCase
1 parent 25bc206 commit c643147

File tree

1 file changed

+33
-32
lines changed

1 file changed

+33
-32
lines changed

main.go

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func main() {
2424
if len(args) != 3 && len(args) != 2 {
2525
// In case provided arguments doesn't match the
2626
// application usage, print application usage message.
27-
printHelp()
27+
PrintHelp()
2828
return
2929
}
3030

@@ -35,19 +35,19 @@ func main() {
3535
if len(args) == 2 {
3636
iniPath = args[1]
3737
} else {
38-
iniPath = defaultWinSCPIniFilePath()
38+
iniPath = GetDefaultWinSCPIniFilePath()
3939
}
40-
decryptIni(iniPath)
40+
DecryptIni(iniPath)
4141
return
4242
}
4343

4444
// In case any argument matches a different operation,
4545
// perform the default decryption operation.
46-
fmt.Println(decrypt(args[0], args[1], args[2]))
46+
fmt.Println(Decrypt(args[0], args[1], args[2]))
4747
}
4848

49-
// Prints a help message with instructions about the application usage.
50-
func printHelp() {
49+
// PrintHelp prints a help message with instructions about the application usage.
50+
func PrintHelp() {
5151
fmt.Println("WinSCP stored password finder")
5252

5353
// WinSCP's password manual decryption mode.
@@ -66,21 +66,21 @@ func printHelp() {
6666
} else {
6767
fmt.Println(" Usage ./winscppasswd ini [<filepath>]")
6868
}
69-
fmt.Printf(" Default value <filepath>: %s\n", defaultWinSCPIniFilePath())
69+
fmt.Printf(" Default value <filepath>: %s\n", GetDefaultWinSCPIniFilePath())
7070
return
7171
}
7272

73-
// Obtains default WinSCP configuration file.
74-
func defaultWinSCPIniFilePath() string {
73+
// GetDefaultWinSCPIniFilePath obtains default WinSCP configuration file.
74+
func GetDefaultWinSCPIniFilePath() string {
7575
usr, err := user.Current()
7676
if err != nil {
7777
log.Fatal(err)
7878
}
7979
return usr.HomeDir + "\\AppData\\Roaming\\winSCP.ini"
8080
}
8181

82-
// Decrypts all entries from a WinSCP's ini file.
83-
func decryptIni(filepath string) {
82+
// DecryptIni decrypts all entries from a WinSCP's ini file.
83+
func DecryptIni(filepath string) {
8484
cfg, err := ini.InsensitiveLoad(filepath)
8585
if err != nil {
8686
panic(err)
@@ -93,23 +93,24 @@ func decryptIni(filepath string) {
9393
fmt.Printf("%s\n", name)
9494
fmt.Printf(" Hostname: %s\n", c.Key("HostName").Value())
9595
fmt.Printf(" Username: %s\n", c.Key("UserName").Value())
96-
fmt.Printf(" Password: %s\n", decrypt(c.Key("HostName").Value(), c.Key("UserName").Value(), c.Key("Password").Value()))
96+
fmt.Printf(" Password: %s\n", Decrypt(c.Key("HostName").Value(), c.Key("UserName").Value(), c.Key("Password").Value()))
9797
fmt.Println("========================")
9898
}
9999
}
100100

101101
}
102102

103-
func decrypt(host, username, password string) string {
103+
// Decrypt decripts a specific server password.
104+
func Decrypt(host, username, password string) string {
104105
// Build 'encryptedPasswordBytes' variable.
105-
encryptedPasswordBytes := getCryptedPasswordBytes(password)
106+
encryptedPasswordBytes := GetCryptedPasswordBytes(password)
106107

107108
// Extract 'flag' and 'cryptedPasswordlength' variables
108-
flag, encryptedPasswordBytes := decryptNextCharacter(encryptedPasswordBytes) // decryptNextCharacter alters the encryptedPasswordBytes variable to remove already parsed characters.
109-
cryptedPasswordlength, encryptedPasswordBytes := getCryptedPasswordLength(flag, encryptedPasswordBytes)
109+
flag, encryptedPasswordBytes := DecryptNextCharacter(encryptedPasswordBytes) // decryptNextCharacter alters the encryptedPasswordBytes variable to remove already parsed characters.
110+
cryptedPasswordlength, encryptedPasswordBytes := GetCryptedPasswordLength(flag, encryptedPasswordBytes)
110111

111112
// Build 'clearpass' variable
112-
clearpass := getPassword(cryptedPasswordlength, encryptedPasswordBytes)
113+
clearpass := GetPassword(cryptedPasswordlength, encryptedPasswordBytes)
113114

114115
// Apply correction to the 'clearpass' variable.
115116
if flag == PasswordFlag {
@@ -121,8 +122,8 @@ func decrypt(host, username, password string) string {
121122
return clearpass
122123
}
123124

124-
// Obtains the crypted password byte array.
125-
func getCryptedPasswordBytes(password string) []byte {
125+
// GetCryptedPasswordBytes obtains the crypted password byte array.
126+
func GetCryptedPasswordBytes(password string) []byte {
126127
encryptedPasswordBytes := []byte{}
127128
for i := 0; i < len(password); i++ {
128129
val, _ := strconv.ParseInt(string(password[i]), 16, 8)
@@ -131,35 +132,35 @@ func getCryptedPasswordBytes(password string) []byte {
131132
return encryptedPasswordBytes
132133
}
133134

134-
// Obtains crypted password length from crypted password byte array.
135-
func getCryptedPasswordLength(flag byte, encryptedPasswordBytes []byte) (byte, []byte) {
135+
// GetCryptedPasswordLength obtains crypted password length from crypted password byte array.
136+
func GetCryptedPasswordLength(flag byte, encryptedPasswordBytes []byte) (byte, []byte) {
136137
var cryptedPasswordlength byte = 0
137138
if flag == PasswordFlag {
138-
_, encryptedPasswordBytes = decryptNextCharacter(encryptedPasswordBytes) // Ignore two characters of the encryptedPasswordBytes.
139-
cryptedPasswordlength, encryptedPasswordBytes = decryptNextCharacter(encryptedPasswordBytes) // decryptNextCharacter alters the encryptedPasswordBytes variable to remove already parsed characters.
139+
_, encryptedPasswordBytes = DecryptNextCharacter(encryptedPasswordBytes) // Ignore two characters of the encryptedPasswordBytes.
140+
cryptedPasswordlength, encryptedPasswordBytes = DecryptNextCharacter(encryptedPasswordBytes) // decryptNextCharacter alters the encryptedPasswordBytes variable to remove already parsed characters.
140141
} else {
141142
cryptedPasswordlength = flag
142143
}
143-
toBeDeleted, encryptedPasswordBytes := decryptNextCharacter(encryptedPasswordBytes) // decryptNextCharacter alters the encryptedPasswordBytes variable to remove already parsed characters.
144+
toBeDeleted, encryptedPasswordBytes := DecryptNextCharacter(encryptedPasswordBytes) // decryptNextCharacter alters the encryptedPasswordBytes variable to remove already parsed characters.
144145
encryptedPasswordBytes = encryptedPasswordBytes[toBeDeleted*2:]
145146
return cryptedPasswordlength, encryptedPasswordBytes
146147
}
147148

148-
// Obtains clear password from crypted password byte array
149-
func getPassword(cryptedPasswordlength byte, encryptedPasswordBytes []byte) string {
149+
// GetPassword obtains clear password from crypted password byte array
150+
func GetPassword(cryptedPasswordlength byte, encryptedPasswordBytes []byte) string {
150151
var i, character byte
151152
var decryptedPassword string
152153

153154
for i = 0; i < cryptedPasswordlength; i++ {
154-
character, encryptedPasswordBytes = decryptNextCharacter(encryptedPasswordBytes) // decryptNextCharacter alters the encryptedPasswordBytes variable to remove already parsed characters.
155+
character, encryptedPasswordBytes = DecryptNextCharacter(encryptedPasswordBytes) // decryptNextCharacter alters the encryptedPasswordBytes variable to remove already parsed characters.
155156
decryptedPassword += string(character) // Add decrypted character to the result variable.
156157
}
157158
return decryptedPassword
158159
}
159160

160-
// Decrypts next character from byte array.
161+
// DecryptNextCharacter decrypts next character from byte array.
161162
// Alters the byte array to remove already parsed bytes.
162-
func decryptNextCharacter(encryptedPasswordBytes []byte) (byte, []byte) {
163+
func DecryptNextCharacter(encryptedPasswordBytes []byte) (byte, []byte) {
163164
if len(encryptedPasswordBytes) <= 0 {
164165
// In case encryptedPasswordBytes param was empty,
165166
// stop the flow here returning '0'.
@@ -169,10 +170,10 @@ func decryptNextCharacter(encryptedPasswordBytes []byte) (byte, []byte) {
169170
a := encryptedPasswordBytes[0] // Obtain first character to parse.
170171
b := encryptedPasswordBytes[1] // Obtain second character to parse.
171172
encryptedPasswordBytes = encryptedPasswordBytes[2:] // Remove already parsed characters.
172-
return decryptCharacter(a, b), encryptedPasswordBytes
173+
return DecryptCharacter(a, b), encryptedPasswordBytes
173174
}
174175

175-
// Decrypts character from two bytes.
176-
func decryptCharacter(a, b byte) byte {
176+
// DecryptCharacter decrypts character from two bytes.
177+
func DecryptCharacter(a, b byte) byte {
177178
return ^(((a << 4) + b) ^ PasswordMagic) & PasswordFlag
178179
}

0 commit comments

Comments
 (0)