-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add hashing feature & increase codecov (#19)
* Add hashing feature & increase codecov * Fix version command
- Loading branch information
Showing
14 changed files
with
416 additions
and
18 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
Copyright © 2024 NAME HERE <EMAIL ADDRESS> | ||
*/ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/ahelmy/xdev/internal" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var validAlgorithms = []string{"md5", "sha256", "sha512", "salt"} | ||
|
||
func validateAlgorithm(algorithm string) bool { | ||
for _, a := range validAlgorithms { | ||
if a == algorithm { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// hashCmd represents the hash command | ||
var hashCmd = &cobra.Command{ | ||
Use: "hash", | ||
Short: "Hash a string", | ||
Long: `Hash a string. For example: | ||
hash -a md5 "abc"`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if len(args) < 1 { | ||
fmt.Println("Please provide an algorithm and a string to hash") | ||
return | ||
} | ||
algorithm, err := cmd.Flags().GetString("algorithm") | ||
if err != nil { | ||
algorithm = "md5" | ||
} | ||
if !validateAlgorithm(algorithm) { | ||
fmt.Println("Please provide a valid algorithm, valid algorithms are: " + strings.Join(validAlgorithms, ", ")) | ||
return | ||
} | ||
result, err := internal.HashString(args[0], algorithm) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
fmt.Println(result) | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(hashCmd) | ||
hashCmd.Flags().StringP("algorithm", "a", "md5", "Hash algorithm") | ||
|
||
// Here you will define your flags and configuration settings. | ||
|
||
// Cobra supports Persistent Flags which will work for this command | ||
// and all subcommands, e.g.: | ||
// hashCmd.PersistentFlags().String("foo", "", "A help for foo") | ||
|
||
// Cobra supports local flags which will only run when this command | ||
// is called directly, e.g.: | ||
// hashCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package internal | ||
|
||
import ( | ||
"crypto" | ||
"encoding/hex" | ||
"fmt" | ||
|
||
"golang.org/x/crypto/bcrypt" | ||
) | ||
|
||
func HashString(str string, algorithm string) (string, error) { | ||
switch algorithm { | ||
case "md5": | ||
return hash(str, crypto.MD5) | ||
case "sha256": | ||
return hash(str, crypto.SHA256) | ||
case "sha512": | ||
return hash(str, crypto.SHA512) | ||
case "salt": | ||
return salt(str) | ||
} | ||
return str, fmt.Errorf("invalid algorithm") | ||
} | ||
|
||
func hash(str string, algorithm crypto.Hash) (string, error) { | ||
hasher := algorithm.New() | ||
|
||
hasher.Write([]byte(str)) | ||
|
||
hashBytes := hasher.Sum(nil) | ||
|
||
return hex.EncodeToString(hashBytes), nil | ||
} | ||
func salt(str string) (string, error) { | ||
salt, err := bcrypt.GenerateFromPassword([]byte(str), 10) | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(salt), nil | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package internal | ||
|
||
import ( | ||
"crypto/md5" | ||
"crypto/sha256" | ||
"crypto/sha512" | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestHashString(t *testing.T) { | ||
testCases := []struct { | ||
str string | ||
algorithm string | ||
expected string | ||
}{ | ||
{ | ||
str: "Hello, World!", | ||
algorithm: "md5", | ||
expected: fmt.Sprintf("%x", md5.Sum([]byte("Hello, World!"))), | ||
}, | ||
{ | ||
str: "Hello, World!", | ||
algorithm: "sha256", | ||
expected: fmt.Sprintf("%x", sha256.Sum256([]byte("Hello, World!"))), | ||
}, | ||
{ | ||
str: "Hello, World!", | ||
algorithm: "sha512", | ||
expected: fmt.Sprintf("%x", sha512.Sum512([]byte("Hello, World!"))), | ||
}, | ||
|
||
{ | ||
str: "Hello, World!", | ||
algorithm: "invalid", | ||
expected: "invalid algorithm", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
result, err := HashString(tc.str, tc.algorithm) | ||
if err != nil { | ||
if err.Error() != tc.expected { | ||
t.Errorf("Expected error: %s, but got: %s", tc.expected, err.Error()) | ||
} | ||
} else { | ||
if result != tc.expected { | ||
t.Errorf("Expected result: %s, but got: %s", tc.expected, result) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func TestSalt(t *testing.T) { | ||
testCases := []struct { | ||
str string | ||
length int | ||
isError bool | ||
}{ | ||
{ | ||
str: "Hello, World!", | ||
length: 60, | ||
isError: false, | ||
}, | ||
{ | ||
str: "$2a$10$2dIj/VAy0Zhgy6eaNYjfAubIbyP5z2V7e8Qzhyr/xeo56GtQ22kOG$2a$10$2dIj/VAy0Zhgy6eaNYjfAubIbyP5z2V7e8Qzhyr/xeo56GtQ22kOG$2a$10$2dIj/VAy0Zhgy6eaNYjfAubIbyP5z2V7e8Qzhyr/xeo56GtQ22kOG$2a$10$2dIj/VAy0Zhgy6eaNYjfAubIbyP5z2V7e8Qzhyr/xeo56GtQ22kOG$2a$10$2dIj/VAy0Zhgy6eaNYjfAubIbyP5z2V7e8Qzhyr/xeo56GtQ22kOG$2a$10$2dIj/VAy0Zhgy6eaNYjfAubIbyP5z2V7e8Qzhyr/xeo56GtQ22kOG$2a$10$2dIj/VAy0Zhgy6eaNYjfAubIbyP5z2V7e8Qzhyr/xeo56GtQ22kOG$2a$10$2dIj/VAy0Zhgy6eaNYjfAubIbyP5z2V7e8Qzhyr/xeo56GtQ22kOG", | ||
isError: true, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
result, err := HashString(tc.str, "salt") | ||
if err != nil && !tc.isError { | ||
t.Errorf("Unexpected error: %v", err) | ||
} else { | ||
if len(result) != tc.length { | ||
t.Errorf("Expected result: %v, but got: %v", tc.length, len(result)) | ||
} | ||
} | ||
} | ||
} |
This file contains 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
Oops, something went wrong.