Skip to content

Commit

Permalink
cleaned up main.go
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanperrine committed Jan 30, 2024
1 parent 3e4376c commit 8263915
Show file tree
Hide file tree
Showing 5 changed files with 337 additions and 276 deletions.
126 changes: 62 additions & 64 deletions Models/Database/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"strings"

)

func Lookup(db *sql.DB, query string) (map[string]string, error) {
Expand Down Expand Up @@ -52,67 +51,66 @@ func Lookup(db *sql.DB, query string) (map[string]string, error) {
}

func BatchLookup(db *sql.DB, queries []string) (map[string]string, error) {
if len(queries) == 0 {
return nil, errors.New("no queries provided")
}

categorizedQueries := make(map[string][]string)
for _, query := range queries {
hashType, err := Hashes.DetectHashType(query)
if err != nil {
return nil, err
}
categorizedQueries[hashType] = append(categorizedQueries[hashType], query)
}

results := make(map[string]string)
for hashType, hashes := range categorizedQueries {
var columnName string
switch hashType {
case "MD4":
columnName = "MD4"
case "MD5":
columnName = "MD5"
case "SHA1":
columnName = "SHA1"
case "SHA256":
columnName = "SHA256"
case "SHA512":
columnName = "SHA512"
default:
continue
}

placeholders := strings.Repeat("?,", len(hashes))
placeholders = strings.TrimRight(placeholders, ",")
sqlQuery := fmt.Sprintf("SELECT Plaintext, %s FROM GoIris WHERE %s IN (%s)", columnName, columnName, placeholders)

args := make([]interface{}, len(hashes))
for i, q := range hashes {
args[i] = q
}

rows, err := db.Query(sqlQuery, args...)
if err != nil {
return nil, err
}

for rows.Next() {
var plaintext, hash string
if err := rows.Scan(&plaintext, &hash); err != nil {
rows.Close()
return nil, err
}
results[hash] = plaintext
}

if err := rows.Err(); err != nil {
rows.Close()
return nil, err
}
rows.Close()
}

return results, nil
}
if len(queries) == 0 {
return nil, errors.New("no queries provided")
}

categorizedQueries := make(map[string][]string)
for _, query := range queries {
hashType, err := Hashes.DetectHashType(query)
if err != nil {
return nil, err
}
categorizedQueries[hashType] = append(categorizedQueries[hashType], query)
}

results := make(map[string]string)
for hashType, hashes := range categorizedQueries {
var columnName string
switch hashType {
case "MD4":
columnName = "MD4"
case "MD5":
columnName = "MD5"
case "SHA1":
columnName = "SHA1"
case "SHA256":
columnName = "SHA256"
case "SHA512":
columnName = "SHA512"
default:
continue
}

placeholders := strings.Repeat("?,", len(hashes))
placeholders = strings.TrimRight(placeholders, ",")
sqlQuery := fmt.Sprintf("SELECT Plaintext, %s FROM GoIris WHERE %s IN (%s)", columnName, columnName, placeholders)

args := make([]interface{}, len(hashes))
for i, q := range hashes {
args[i] = q
}

rows, err := db.Query(sqlQuery, args...)
if err != nil {
return nil, err
}

for rows.Next() {
var plaintext, hash string
if err := rows.Scan(&plaintext, &hash); err != nil {
rows.Close()
return nil, err
}
results[hash] = plaintext
}

if err := rows.Err(); err != nil {
rows.Close()
return nil, err
}
rows.Close()
}

return results, nil
}
140 changes: 140 additions & 0 deletions Models/Modules/dehashFile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package Modules

import (
"GoIris/Models"
"GoIris/Models/Database"
"bufio"
"fmt"
"os"
"strings"
"time"

"github.com/sqweek/dialog"
)

type ItemHashPair struct {
Item string
Hash string
}

func DehashFile() error {
file, err := openInputFile()
if err != nil {
return err
}
defer file.Close()

crackedOutFile, nonCrackedOutFile, err := createOutputFiles()
if err != nil {
return err
}
defer crackedOutFile.Close()
defer nonCrackedOutFile.Close()

return processDehashInputFile(file, crackedOutFile, nonCrackedOutFile)
}

func openInputFile() (*os.File, error) {
filePath, err := dialog.File().Load()
if err != nil {
return nil, fmt.Errorf("error opening file dialog: %w", err)
}

file, err := os.Open(filePath)
if err != nil {
return nil, fmt.Errorf("error opening file: %w", err)
}

return file, nil
}

func createOutputFiles() (*os.File, *os.File, error) {
outputDir := fmt.Sprintf("Output/%s", time.Now().Format("2006-01-02 [15 04 05]"))

err := os.MkdirAll(outputDir, os.ModePerm)
if err != nil {
return nil, nil, fmt.Errorf("error creating output directory: %w", err)
}

crackedFile := fmt.Sprintf("%s/Cracked.txt", outputDir)
crackedOutFile, err := os.Create(crackedFile)
if err != nil {
return nil, nil, fmt.Errorf("error creating cracked output file: %w", err)
}

nonCrackedFile := fmt.Sprintf("%s/NonCracked.txt", outputDir)
nonCrackedOutFile, err := os.Create(nonCrackedFile)
if err != nil {
crackedOutFile.Close()
return nil, nil, fmt.Errorf("error creating non-cracked output file: %w", err)
}

return crackedOutFile, nonCrackedOutFile, nil
}

func processDehashInputFile(file *os.File, crackedOutFile, nonCrackedOutFile *os.File) error {
scanner := bufio.NewScanner(file)
const batchSize = 512
var batch []ItemHashPair

for scanner.Scan() {
line := scanner.Text()
pair, err := parseLine(line)
if err != nil {
fmt.Println(err)
continue
}

batch = append(batch, pair)
if len(batch) >= batchSize {
if err := processBatch(batch, crackedOutFile, nonCrackedOutFile); err != nil {
return err
}
batch = []ItemHashPair{}
}
}

if len(batch) > 0 {
if err := processBatch(batch, crackedOutFile, nonCrackedOutFile); err != nil {
return err
}
}

if err := scanner.Err(); err != nil {
return fmt.Errorf("error reading file: %w", err)
}

return nil
}

func parseLine(line string) (ItemHashPair, error) {
parts := strings.SplitN(line, ":", 2)
if len(parts) != 2 {
return ItemHashPair{}, fmt.Errorf("invalid line format: %s", line)
}
return ItemHashPair{Item: parts[0], Hash: parts[1]}, nil
}

func processBatch(batch []ItemHashPair, crackedOutFile, nonCrackedOutFile *os.File) error {
var hashBatch []string
for _, pair := range batch {
hashBatch = append(hashBatch, pair.Hash)
}

dehashedResults, _ := Database.BatchLookup(Models.DB, hashBatch)

for _, pair := range batch {
plaintext, found := dehashedResults[pair.Hash]
if found && plaintext != "" {
if _, err := fmt.Fprintf(crackedOutFile, "%s:%s:%s\n", pair.Item, pair.Hash, plaintext); err != nil {
fmt.Printf("Error writing to cracked output file: %v\n", err)
}
} else {
if _, err := fmt.Fprintf(nonCrackedOutFile, "%s:%s\n", pair.Item, pair.Hash); err != nil {
fmt.Printf("Error writing to non-cracked output file: %v\n", err)
}
}
}

return nil
}
95 changes: 95 additions & 0 deletions Models/Modules/inputFile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package Modules

import (
"GoIris/Models"
"GoIris/Models/Database"
"GoIris/Models/Hashes"
"bufio"
"fmt"
"os"
"strings"

)

func InsertFile() error {
file, err := openInputFile()
if err != nil {
return err
}
defer file.Close()

if err := checkDiskSpace(file); err != nil {
return err
}

return processInputFile(file)
}

func checkDiskSpace(file *os.File) error {
freeSpace := int64(Models.CheckDiskSpace())
md4size, md5Size, sha1Size, sha256Size, sha512Size := Models.CalculateFileHashSizes(file)
totalSize := int64(md4size + md5Size + sha1Size + sha256Size + sha512Size)

if freeSpace < totalSize {
fmt.Println("Warning: Not enough free space to insert file.")
fmt.Println("Continuing may cause database corruption due to running out of space.")
fmt.Print("Do you want to continue? (yes/no): ")

reader := bufio.NewReader(os.Stdin)
response, err := reader.ReadString('\n')
if err != nil {
return fmt.Errorf("error reading input: %w", err)
}
response = strings.TrimSpace(response)

if strings.ToLower(response) != "yes" && strings.ToLower(response) != "y" {
fmt.Println("Operation aborted.")
return fmt.Errorf("operation aborted by user")
}
}

fmt.Println("Minimum Size Of DB:", Models.ConvertBytesToPretty(totalSize))
return nil
}

func processInputFile(file *os.File) error {
scanner := bufio.NewScanner(file)
var lines []string
const batchSize = 512
hashOptions := Hashes.HashOptions{
IncludeMD4: true,
IncludeMD5: true,
IncludeSHA1: true,
IncludeSHA256: true,
IncludeSHA512: true,
}

for scanner.Scan() {
lines = append(lines, scanner.Text())
if len(lines) == batchSize {
if err := insertBatch(lines, hashOptions); err != nil {
return err
}
lines = []string{}
}
}

if len(lines) > 0 {
if err := insertBatch(lines, hashOptions); err != nil {
return err
}
}

if err := scanner.Err(); err != nil {
return fmt.Errorf("error scanning file: %w", err)
}

return nil
}

func insertBatch(lines []string, hashOptions Hashes.HashOptions) error {
if err := Database.InsertBatchString(Models.DB, lines, hashOptions); err != nil {
return fmt.Errorf("error inserting batch data: %w", err)
}
return nil
}
Loading

0 comments on commit 8263915

Please sign in to comment.