Last active
April 27, 2021 22:46
-
-
Save xlbruce/2c9823dda2dbad85377f91772588a38a to your computer and use it in GitHub Desktop.
Primeiro programa em Go. Tenta descriptografar um PDF cuja senha seja de 3 dígitos numéricos, normalmente enviados pela [Vivo](https://vivo.com.br).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
//TODO publish to github reporitory | |
import ( | |
"fmt" | |
"log" | |
"os" | |
"rsc.io/pdf" | |
) | |
func GeneratePasswords(result *string) func() string { | |
i := 0 | |
return func() string { | |
if i == 999 { | |
log.Fatal("Máximo número de tentativas. Não foi possível descriptografar o arquivo. (999)\n") | |
return "" | |
} | |
i++ | |
p := fmt.Sprintf("%03d", i) | |
*result = p | |
return p | |
} | |
} | |
func usage() string { | |
//TODO improve binary execution | |
return `go run main.go <pdf_filename>` | |
} | |
func main() { | |
if len(os.Args) < 2 { | |
log.Fatal(usage()) | |
} | |
var password string | |
filename := os.Args[1] | |
f, err := os.Open(filename) | |
if err != nil { | |
msg := fmt.Sprintf("Erro ao abrir o arquivo: %s\n", err.Error()) | |
log.Fatal(msg) | |
} | |
stat, _ := f.Stat() | |
_, err = pdf.NewReaderEncrypted(f, stat.Size(), GeneratePasswords(&password)) | |
if err != nil { | |
msg := fmt.Sprintf("Arquivo PDF inválido: %s", err.Error()) | |
log.Fatal(msg) | |
} | |
if password == "" { | |
fmt.Printf("O arquivo não é protegido por senha.\n") | |
} else { | |
fmt.Printf("A senha do PDF %s é: %s\n", filename, password) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment