Skip to content

Commit

Permalink
Add sha-family of hashers (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
joohoi authored May 13, 2021
1 parent f107aa2 commit 1f38d24
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/pencode/encoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ var availableEncoders = map[string]Encoder{
"jsonescape": JSONEscaper{},
"jsonunescape": JSONUnescaper{},
"md5": MD5Hasher{},
"sha1": SHA1Hasher{},
"sha224": SHA224Hasher{},
"sha256": SHA256Hasher{},
"sha384": SHA384Hasher{},
"sha512": SHA512Hasher{},
"unicodedecode": UnicodeDecode{},
"unicodeencodeall": UnicodeEncodeAll{},
"urlencode": URLEncoder{},
Expand Down
16 changes: 16 additions & 0 deletions pkg/pencode/sha1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package pencode

import (
"crypto/sha1"
"fmt"
)

type SHA1Hasher struct{}

func (m SHA1Hasher) Encode(input []byte) ([]byte, error) {
return []byte(fmt.Sprintf("%x", sha1.Sum(input))), nil
}

func (m SHA1Hasher) HelpText() string {
return "SHA1 checksum"
}
16 changes: 16 additions & 0 deletions pkg/pencode/sha224.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package pencode

import (
"crypto/sha256"
"fmt"
)

type SHA224Hasher struct{}

func (m SHA224Hasher) Encode(input []byte) ([]byte, error) {
return []byte(fmt.Sprintf("%x", sha256.Sum224(input))), nil
}

func (m SHA224Hasher) HelpText() string {
return "SHA224 checksum"
}
16 changes: 16 additions & 0 deletions pkg/pencode/sha256.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package pencode

import (
"crypto/sha256"
"fmt"
)

type SHA256Hasher struct{}

func (m SHA256Hasher) Encode(input []byte) ([]byte, error) {
return []byte(fmt.Sprintf("%x", sha256.Sum256(input))), nil
}

func (m SHA256Hasher) HelpText() string {
return "SHA256 checksum"
}
16 changes: 16 additions & 0 deletions pkg/pencode/sha384.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package pencode

import (
"crypto/sha512"
"fmt"
)

type SHA384Hasher struct{}

func (m SHA384Hasher) Encode(input []byte) ([]byte, error) {
return []byte(fmt.Sprintf("%x", sha512.Sum384(input))), nil
}

func (m SHA384Hasher) HelpText() string {
return "SHA384 checksum"
}
16 changes: 16 additions & 0 deletions pkg/pencode/sha512.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package pencode

import (
"crypto/sha512"
"fmt"
)

type SHA512Hasher struct{}

func (m SHA512Hasher) Encode(input []byte) ([]byte, error) {
return []byte(fmt.Sprintf("%x", sha512.Sum512(input))), nil
}

func (m SHA512Hasher) HelpText() string {
return "SHA512 checksum"
}

0 comments on commit 1f38d24

Please sign in to comment.