Skip to content

Commit c0dc9b0

Browse files
committed
hashing tools
1 parent 02b08f7 commit c0dc9b0

File tree

5 files changed

+77
-0
lines changed

5 files changed

+77
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ devd convert to_upper_case [input]
110110
# devd c uppercase aa
111111
```
112112

113+
### Hashing tools
114+
115+
```bash
116+
devd hash md5 [input]
117+
devd hash keccak256 [input]
118+
```
119+
113120
### Debug tools
114121

115122
#### Compute EVM transaction intrinsic gas

cmd/hash/keccak256.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package hash
2+
3+
import (
4+
"encoding/hex"
5+
"fmt"
6+
"github.com/ethereum/go-ethereum/crypto"
7+
"github.com/spf13/cobra"
8+
"strings"
9+
)
10+
11+
func GetKeccak256Command() *cobra.Command {
12+
cmd := &cobra.Command{
13+
Use: "keccak256 [input]",
14+
Short: "keccak256 hashing input",
15+
Args: cobra.ExactArgs(1),
16+
Run: func(_ *cobra.Command, args []string) {
17+
input := strings.Join(args, " ")
18+
hash := crypto.Keccak256Hash([]byte(input))
19+
fmt.Println(hex.EncodeToString(hash.Bytes()))
20+
},
21+
}
22+
23+
return cmd
24+
}

cmd/hash/md5.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package hash
2+
3+
import (
4+
"crypto/md5"
5+
"encoding/hex"
6+
"fmt"
7+
"github.com/spf13/cobra"
8+
"strings"
9+
)
10+
11+
func GetMd5Command() *cobra.Command {
12+
cmd := &cobra.Command{
13+
Use: "md5 [input]",
14+
Short: "MD5 hashing input",
15+
Args: cobra.ExactArgs(1),
16+
Run: func(_ *cobra.Command, args []string) {
17+
input := strings.Join(args, " ")
18+
hash := md5.Sum([]byte(input))
19+
fmt.Println(hex.EncodeToString(hash[:]))
20+
},
21+
}
22+
23+
return cmd
24+
}

cmd/hash/root.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package hash
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
)
6+
7+
// Commands registers a sub-tree of commands
8+
func Commands() *cobra.Command {
9+
cmd := &cobra.Command{
10+
Use: "hash",
11+
Short: "Hashing commands",
12+
}
13+
14+
cmd.AddCommand(
15+
GetMd5Command(),
16+
GetKeccak256Command(),
17+
)
18+
19+
return cmd
20+
}

cmd/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
libutils "github.com/EscanBE/go-lib/utils"
77
"github.com/bcdevtools/devd/cmd/convert"
88
"github.com/bcdevtools/devd/cmd/debug"
9+
"github.com/bcdevtools/devd/cmd/hash"
910
"github.com/bcdevtools/devd/cmd/query"
1011
"github.com/bcdevtools/devd/cmd/types"
1112
"github.com/bcdevtools/devd/cmd/utils"
@@ -60,6 +61,7 @@ func init() {
6061
rootCmd.AddCommand(convert.Commands())
6162
rootCmd.AddCommand(debug.Commands())
6263
rootCmd.AddCommand(query.Commands())
64+
rootCmd.AddCommand(hash.Commands())
6365

6466
rootCmd.PersistentFlags().String(constants.FLAG_USE_WORKING_USERNAME, "", "Use the specified username as working context username, must be either effective user or real user, if not specified, will use default selected working user")
6567
rootCmd.PersistentFlags().String(constants.FLAG_REQUIRE_WORKING_USERNAME, "", "Ensure working user is the specified user: if working user selected by application has username different with the specified username, application will exit with error")

0 commit comments

Comments
 (0)