hash

package
v0.1.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 4, 2023 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BcryptCmd = &cli.Command{
	Category: "Crypto",
	Name:     "bcrypt",
	Usage:    "Bcrypt Generate/Compare",
	Commands: []*cli.Command{
		&cli.Command{
			Name:    "generate",
			Aliases: []string{"gen", "g"},
			Usage:   "Generate",
			Flags: []cli.Flag{
				&cli.IntFlag{
					Name:    "cost",
					Aliases: []string{"c"},
					Usage:   "bcrypt cost",
					Value:   bcrypt.DefaultCost,
					Action: func(c *cli.Context, cost int) error {
						if cost < bcrypt.MinCost || cost > bcrypt.MaxCost {
							return fmt.Errorf("cost must be between %d and %d", bcrypt.MinCost, bcrypt.MaxCost)
						}
						return nil
					},
				},
			},
			Action: func(c *cli.Context) (err error) {
				code, err := utils.ArgOrPipe(c.Args().First())
				if err != nil {
					return err
				}
				r, err := BcryptGenerate(code, c.Int("cost"))
				if err != nil {
					return err
				}
				_, err = fmt.Println(r)
				return
			},
		},
		&cli.Command{
			Name:      "compare",
			Aliases:   []string{"comp", "c"},
			Usage:     "Compare",
			ArgsUsage: "hashed password",
			Action: func(c *cli.Context) (err error) {
				if c.NArg() < 2 {
					return fmt.Errorf("missing parameter")
				}
				err = BcryptCompare(c.Args().Get(0), c.Args().Get(1))
				if err != nil {
					return err
				}
				logger.Success("Compare OK")
				return nil
			},
		},
	},
}
View Source
var Cmd = &cli.Command{
	Category: "Crypto",
	Name:     "hash",
	Usage:    "Hash Function",
	Commands: []*cli.Command{

		&cli.Command{
			Category: "Hash",
			Name:     "md5",
			Usage:    "MD5 algorithm",
			Flags:    flags,
			Action: func(c *cli.Context) (err error) {
				code, err := utils.GetArgFilePipe(c.Args().First())
				if err != nil {
					return err
				}
				key := c.String("hmac")
				var r string
				if key != "" {
					r, _ = HmacMDHash(code, []byte(key), 5)
				} else {
					r, _ = MDHash(code, 5, c.Bool("raw"))
				}
				if c.Bool("raw") {
					_, err = fmt.Print(r)
					return
				}
				_, err = fmt.Println(r)
				return
			},
		},

		&cli.Command{
			Category: "Hash",
			Name:     "md4",
			Usage:    "MD4 algorithm",
			Flags:    flags,
			Action: func(c *cli.Context) (err error) {
				code, err := utils.GetArgFilePipe(c.Args().First())
				if err != nil {
					return err
				}
				key := c.String("hmac")
				var r string
				if key != "" {
					r, _ = HmacMDHash(code, []byte(key), 4)
				} else {
					r, _ = MDHash(code, 4, c.Bool("raw"))
				}
				if c.Bool("raw") {
					_, err = fmt.Print(r)
					return
				}
				_, err = fmt.Println(r)
				return
			},
		},

		&cli.Command{
			Category: "Hash",
			Name:     "md2",
			Usage:    "MD2 algorithm",
			Flags:    flags,
			Action: func(c *cli.Context) (err error) {
				code, err := utils.GetArgFilePipe(c.Args().First())
				if err != nil {
					return err
				}
				key := c.String("hmac")
				var r string
				if key != "" {
					r, _ = HmacMDHash(code, []byte(key), 2)
				} else {
					r, _ = MDHash(code, 2, c.Bool("raw"))
				}
				if c.Bool("raw") {
					_, err = fmt.Print(r)
					return
				}
				_, err = fmt.Println(r)
				return
			},
		},

		&cli.Command{
			Category: "Hash",
			Name:     "sha1",
			Usage:    "SHA1 algorithm",
			Flags:    flags,
			Action: func(c *cli.Context) (err error) {
				code, err := utils.GetArgFilePipe(c.Args().First())
				if err != nil {
					return err
				}
				key := c.String("hmac")
				var r string
				if key != "" {
					r, _ = HmacSha1Hash(code, []byte(key))
				} else {
					r, _ = Sha1Hash(code, c.Bool("raw"))
				}
				if c.Bool("raw") {
					_, err = fmt.Print(r)
					return
				}
				_, err = fmt.Println(r)
				return
			},
		},

		&cli.Command{
			Category: "Hash",
			Name:     "sha2",
			Usage:    "SHA2 224|256|384|512|512224|512256",
			Flags: append(flags,
				&cli.IntFlag{
					Name:    "type",
					Aliases: []string{"t"},
					Usage:   "Type of hash",
					Value:   256,
					Action: func(c *cli.Context, t int) error {
						if t == 224 || t == 256 || t == 384 || t == 512 ||
							t == 512224 || t == 512256 {
							return nil
						}
						return fmt.Errorf("invalid type: %d", t)
					},
				},
			),
			Action: func(c *cli.Context) (err error) {
				code, err := utils.GetArgFilePipe(c.Args().First())
				if err != nil {
					return err
				}
				key := c.String("hmac")
				var r string
				if key != "" {
					r, _ = HmacSha2Hash(code, []byte(key), c.Int("type"))
				} else {
					r, _ = Sha2Hash(code, c.Int("type"))
				}
				_, err = fmt.Println(r)
				return
			},
		},

		&cli.Command{
			Category: "Hash",
			Name:     "sha3",
			Usage:    "SHA3 224|256|384|512",
			Flags: append(flags,
				&cli.IntFlag{
					Name:    "type",
					Aliases: []string{"t"},
					Usage:   "Type of hash",
					Value:   256,
					Action: func(c *cli.Context, s int) error {
						if s == 224 || s == 256 || s == 384 || s == 512 {
							return nil
						}
						return fmt.Errorf("invalid size: %d", s)
					},
				},
			),
			Action: func(c *cli.Context) (err error) {
				code, err := utils.GetArgFilePipe(c.Args().First())
				if err != nil {
					return err
				}
				key := c.String("hmac")
				var r string
				if key != "" {
					r, _ = HmacSha3Hash(code, []byte(key), c.Int("type"))
				} else {
					r, _ = Sha3Hash(code, c.Int("type"))
				}
				_, err = fmt.Println(r)
				return
			},
		},

		&cli.Command{
			Category: "Hash",
			Name:     "ripemd",
			Aliases:  []string{"ripemd160"},
			Usage:    "RIPEMD160 algorithm",
			Flags: append(flags,
				&cli.IntFlag{
					Name:    "type",
					Aliases: []string{"t"},
					Usage:   "Type of hash",
					Value:   256,
					Action: func(c *cli.Context, s int) error {
						if s == 224 || s == 256 || s == 384 || s == 512 {
							return nil
						}
						return fmt.Errorf("invalid size: %d", s)
					},
				},
			),
			Action: func(c *cli.Context) (err error) {
				code, err := utils.GetArgFilePipe(c.Args().First())
				if err != nil {
					return err
				}
				key := c.String("hmac")
				var r string
				if key != "" {
					r, _ = HmacRipemd160Hash(code, []byte(key))
				} else {
					r, _ = Ripemd160Hash(code)
				}
				_, err = fmt.Println(r)
				return
			},
		},

		&cli.Command{
			Category: "Hash",
			Name:     "mysql",
			Usage:    "MySQL Hash password using before 4.1",
			Action: func(c *cli.Context) (err error) {
				code, err := utils.GetArgFilePipe(c.Args().First())
				if err != nil {
					return err
				}
				_, err = fmt.Println(MySQLHash(code))
				return
			},
		},

		&cli.Command{
			Category: "Hash",
			Name:     "mysql5",
			Usage:    "MySQL5 Hash password using 4.1+ method (SHA1)",
			Action: func(c *cli.Context) (err error) {
				code, err := utils.GetArgFilePipe(c.Args().First())
				if err != nil {
					return err
				}
				_, err = fmt.Println(MySQL5Hash(code))
				return
			},
		},

		&cli.Command{
			Category: "Hash",
			Name:     "ntlm",
			Aliases:  []string{"ntlm"},
			Usage:    "NTLM Hash password (MD4(utf16))",
			Action: func(c *cli.Context) (err error) {
				code, err := utils.GetArgFilePipe(c.Args().First())
				if err != nil {
					return err
				}
				_, err = fmt.Println(NTLMv1Hash(code))
				return
			},
		},
	},
}

Functions

func BcryptCompare added in v0.1.6

func BcryptCompare(hashed, passwd string) error

func BcryptGenerate added in v0.1.6

func BcryptGenerate(s string, cost int) (string, error)

func HmacMDHash added in v0.1.6

func HmacMDHash(s, key []byte, typ int) (string, error)

func HmacRipemd160Hash added in v0.1.6

func HmacRipemd160Hash(s, key []byte) (string, error)

func HmacSha1Hash added in v0.1.6

func HmacSha1Hash(s, key []byte) (string, error)

func HmacSha2Hash added in v0.1.6

func HmacSha2Hash(s, key []byte, size int) (string, error)

func HmacSha3Hash added in v0.1.6

func HmacSha3Hash(s, key []byte, size int) (string, error)

func MDHash added in v0.1.6

func MDHash(s []byte, typ int, isRaw ...bool) (string, error)

func MySQL5Hash added in v0.1.7

func MySQL5Hash(password []byte) string

Generate binary Hash password using 4.1+ method (SHA1)

func MySQLHash added in v0.1.7

func MySQLHash(password []byte) string

Generate binary hash from byte string using insecure pre 4.1 method

func NTLMv1Hash added in v0.1.7

func NTLMv1Hash(s []byte) string

func Ripemd160Hash added in v0.1.6

func Ripemd160Hash(s []byte, isRaw ...bool) (string, error)

func Sha1Hash added in v0.1.6

func Sha1Hash(s []byte, isRaw ...bool) (string, error)

func Sha2Hash added in v0.1.6

func Sha2Hash(b []byte, size int, isRaw ...bool) (string, error)

func Sha3Hash added in v0.1.6

func Sha3Hash(b []byte, size int, isRaw ...bool) (string, error)

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL