Last active
February 11, 2022 04:53
-
-
Save luanvuhlu/db02c91b6ad48fd24f7fc46e3c905a47 to your computer and use it in GitHub Desktop.
Decode sha256 hash with number less than or equal 100000000
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 | |
import ( | |
"bufio" | |
"crypto/sha256" | |
"encoding/hex" | |
"fmt" | |
"log" | |
"os" | |
"strconv" | |
"strings" | |
"time" | |
) | |
func contains(s []string, e string) bool { | |
for _, a := range s { | |
if a == e { | |
return true | |
} | |
} | |
return false | |
} | |
func createAndMatch(hashList []string, s string) (string, bool) { | |
h := sha256.New() | |
h.Write([]byte(s)) | |
sha_hash := hex.EncodeToString(h.Sum(nil)) | |
return sha_hash, contains(hashList, sha_hash) | |
} | |
func process(hashList []string) { | |
count := len(hashList) | |
for i := 99999999; i > 0; i-- { | |
if count == 0 { | |
return | |
} | |
s := strconv.Itoa(i) | |
hash, match := createAndMatch(hashList, s) | |
if match { | |
fmt.Println(hash, "\t", s) | |
count-- | |
} | |
// if i%100000 == 0 || i == 1 { | |
// fmt.Println(s) | |
// } | |
} | |
} | |
func readFile(filePath string) []string { | |
file, err := os.Open(filePath) | |
if err != nil { | |
panic("failed to open") | |
} | |
scanner := bufio.NewScanner(file) | |
scanner.Split(bufio.ScanLines) | |
var text []string | |
for scanner.Scan() { | |
text = append(text, strings.TrimSpace(scanner.Text())) | |
} | |
file.Close() | |
return text | |
} | |
func getHashList(args []string) []string { | |
if len(args[0]) != 64 { | |
return readFile(args[0]) | |
} | |
return args | |
} | |
func elapsed(what string) func() { | |
start := time.Now() | |
return func() { | |
fmt.Printf("%s took %v\n", what, time.Since(start)) | |
} | |
} | |
func main() { | |
args := os.Args[1:] | |
if len(args) == 0 { | |
log.Fatalf("No argument. Are you kidding!") | |
return | |
} | |
hashList := getHashList(args) | |
fmt.Println("Number of hash: ", len(hashList)) | |
fmt.Printf("Finding for %v\n", hashList) | |
fmt.Println("..............") | |
fmt.Println("....................") | |
defer elapsed("Find process")() | |
process(hashList) | |
fmt.Println("------------------------------------------------------------------------------------------------") | |
} |
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 | |
import ( | |
"crypto/sha256" | |
"encoding/hex" | |
"fmt" | |
"strconv" | |
) | |
func contains(s []string, e string) bool { | |
for _, a := range s { | |
if a == e { | |
return true | |
} | |
} | |
return false | |
} | |
func createAndMatch(s string) { | |
h := sha256.New() | |
h.Write([]byte(s)) | |
sha_hash := hex.EncodeToString(h.Sum(nil)) | |
fmt.Println(sha_hash + "," + s) | |
} | |
func process() { | |
for i := 99999999; i > 0; i-- { | |
s := strconv.Itoa(i) | |
createAndMatch(s) | |
} | |
} | |
func main() { | |
process() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment