github.com/mowshon/iterium@v1.0.0/decrypt_test.go (about)

     1  package iterium
     2  
     3  import (
     4  	"crypto/md5"
     5  	"encoding/hex"
     6  	"fmt"
     7  	"strings"
     8  	"testing"
     9  )
    10  
    11  func BenchmarkDecryptMD5Hash(b *testing.B) {
    12  	// result of md5("qwerty") = d8578edf8458ce06fbc5bb76a58c5ca4
    13  	passHash := "d8578edf8458ce06fbc5bb76a58c5ca4"
    14  
    15  	for passLength := range Range(1, 7).Chan() {
    16  		product := Product(AsciiLowercase, passLength)
    17  		fmt.Println("Password Length:", passLength, "total combinations:", product.Count())
    18  
    19  		// Merge a slide into a string.
    20  		// []string{"a", "b", "c"} => "abc"
    21  		join := func(product []string) string {
    22  			return strings.Join(product, "")
    23  		}
    24  
    25  		// Check the hash of a raw password with an unknown hash.
    26  		sameHash := func(rawPassword string) bool {
    27  			hash := md5.Sum([]byte(rawPassword))
    28  			return hex.EncodeToString(hash[:]) == passHash
    29  		}
    30  
    31  		// Combine iterators to achieve the goal...
    32  		decrypt := FirstTrue(
    33  			Map(product, join), sameHash,
    34  		)
    35  
    36  		if result, err := decrypt.Next(); err == nil {
    37  			fmt.Println("Raw password:", result)
    38  			break
    39  		}
    40  	}
    41  }