Skip to content

Commit

Permalink
新增hash加密算法
Browse files Browse the repository at this point in the history
  • Loading branch information
wangluozhe committed Mar 31, 2022
1 parent afc1953 commit 859f7e5
Show file tree
Hide file tree
Showing 8 changed files with 303 additions and 0 deletions.
56 changes: 56 additions & 0 deletions examples/crypto-hmac.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"fmt"
"github.com/wangluozhe/requests/utils"
)

func main() {
md4 := utils.HmacMD4("123", "123")
bs64 := utils.Btoa(md4)
hex := utils.HexEncode(md4)
fmt.Println("HmacMD4-base64:", bs64)
fmt.Println("HmacMD4-hex:", string(hex))

r160 := utils.HmacRIPEMD160("123", "123")
bs64 = utils.Btoa(r160)
hex = utils.HexEncode(r160)
fmt.Println("HmacRIPEMD160-base64:", bs64)
fmt.Println("HmacRIPEMD160-hex:", string(hex))

md5 := utils.HmacMD5("123", "123")
bs64 = utils.Btoa(md5)
hex = utils.HexEncode(md5)
fmt.Println("HmacMD5-base64:", bs64)
fmt.Println("HmacMD5-hex:", string(hex))

sha1 := utils.HmacSHA1("123", "123")
bs64 = utils.Btoa(sha1)
hex1 := utils.HexEncode(sha1)
fmt.Println("HmacSHA1-base64:", bs64)
fmt.Println("HmacSHA1-hex:", string(hex1))

sha224 := utils.HmacSHA224("123", "123")
bs64 = utils.Btoa(sha224)
hex2 := utils.HexEncode(sha224)
fmt.Println("HmacSHA224-base64:", bs64)
fmt.Println("HmacSHA224-hex:", string(hex2))

sha256 := utils.HmacSHA256("123", "123")
bs64 = utils.Btoa(sha256)
hex3 := utils.HexEncode(sha256)
fmt.Println("HmacSHA256-base64:", bs64)
fmt.Println("HmacSHA256-hex:", string(hex3))

sha384 := utils.HmacSHA384("123", "123")
bs64 = utils.Btoa(sha384)
hex4 := utils.HexEncode(sha384)
fmt.Println("HmacSHA384-base64:", bs64)
fmt.Println("HmacSHA384-hex:", string(hex4))

sha512 := utils.HmacSHA512("123", "123")
bs64 = utils.Btoa(sha512)
hex5 := utils.HexEncode(sha512)
fmt.Println("HmacSHA512-base64:", bs64)
fmt.Println("HmacSHA512-hex:", string(hex5))
}
17 changes: 17 additions & 0 deletions examples/crypto-md5-test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import (
"fmt"
"github.com/wangluozhe/requests/utils"
)

func main() {
m := utils.MD4("123")
fmt.Println("MD4:", m)

m = utils.RIPEMD160("123")
fmt.Println("RIPEMD160:", m)

m = utils.MD5("123")
fmt.Println("MD5:", m)
}
12 changes: 12 additions & 0 deletions examples/crypto-rc4-test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
"fmt"
"github.com/wangluozhe/requests/utils"
)

func main() {
rc4 := utils.RC4("123", "123")
fmt.Println("RC4-base64:", utils.Btoa(rc4))
fmt.Println("RC4-hex:",string(utils.HexEncode(rc4)))
}
38 changes: 38 additions & 0 deletions examples/crypto-sha-test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"fmt"
"github.com/wangluozhe/requests/utils"
)

func main() {
s1 := utils.SHA1("123")
b64 := utils.Btoa(s1)
h16 := utils.HexEncode(s1)
fmt.Println("SHA1-base64:", b64)
fmt.Println("SHA1-hex:", string(h16))

s2 := utils.SHA224("123")
b64 = utils.Btoa(s2)
h16 = utils.HexEncode(s2)
fmt.Println("SHA224-base64:", b64)
fmt.Println("SHA224-hex:", string(h16))

s2 = utils.SHA256("123")
b64 = utils.Btoa(s2)
h16 = utils.HexEncode(s2)
fmt.Println("SHA256-base64:", b64)
fmt.Println("SHA256-hex:", string(h16))

s5 := utils.SHA384("123")
b64 = utils.Btoa(s5)
h16 = utils.HexEncode(s5)
fmt.Println("SHA384-base64:", b64)
fmt.Println("SHA384-hex:", string(h16))

s5 = utils.SHA512("123")
b64 = utils.Btoa(s5)
h16 = utils.HexEncode(s5)
fmt.Println("SHA512-base64:", b64)
fmt.Println("SHA512-hex:", string(h16))
}
83 changes: 83 additions & 0 deletions utils/hmac.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package utils

import (
"crypto/hmac"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"golang.org/x/crypto/md4"
"golang.org/x/crypto/ripemd160"
)

// HmacMD4加密
func HmacMD4(s, key interface{}) []byte {
byte_s := stringAndByte(s)
byte_key := stringAndByte(key)
h := hmac.New(md4.New, byte_key)
h.Write(byte_s)
return h.Sum(nil)
}

// HmacRIPEMD160加密
func HmacRIPEMD160(s, key interface{}) []byte {
byte_s := stringAndByte(s)
byte_key := stringAndByte(key)
h := hmac.New(ripemd160.New, byte_key)
h.Write(byte_s)
return h.Sum(nil)
}

// HmacMD5加密
func HmacMD5(s, key interface{}) []byte {
byte_s := stringAndByte(s)
byte_key := stringAndByte(key)
h := hmac.New(md5.New, byte_key)
h.Write(byte_s)
return h.Sum(nil)
}

// HmacSHA1加密
func HmacSHA1(s, key interface{}) []byte {
byte_s := stringAndByte(s)
byte_key := stringAndByte(key)
h := hmac.New(sha1.New, byte_key)
h.Write(byte_s)
return h.Sum(nil)
}

// HmacSHA224加密
func HmacSHA224(s, key interface{}) []byte {
byte_s := stringAndByte(s)
byte_key := stringAndByte(key)
h := hmac.New(sha256.New224, byte_key)
h.Write(byte_s)
return h.Sum(nil)
}

// HmacSHA256加密
func HmacSHA256(s, key interface{}) []byte {
byte_s := stringAndByte(s)
byte_key := stringAndByte(key)
h := hmac.New(sha256.New, byte_key)
h.Write(byte_s)
return h.Sum(nil)
}

// HmacSHA384加密
func HmacSHA384(s, key interface{}) []byte {
byte_s := stringAndByte(s)
byte_key := stringAndByte(key)
h := hmac.New(sha512.New384, byte_key)
h.Write(byte_s)
return h.Sum(nil)
}

// HmacSHA512加密
func HmacSHA512(s, key interface{}) []byte {
byte_s := stringAndByte(s)
byte_key := stringAndByte(key)
h := hmac.New(sha512.New, byte_key)
h.Write(byte_s)
return h.Sum(nil)
}
32 changes: 32 additions & 0 deletions utils/md5.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package utils

import (
"crypto/md5"
"fmt"
"golang.org/x/crypto/md4"
"golang.org/x/crypto/ripemd160"
)

// MD4加密
func MD4(s interface{}) string {
byte_s := stringAndByte(s)
m := md4.New()
m.Write(byte_s)
return fmt.Sprintf("%x", m.Sum(nil))
}

// RIPEMD160加密
func RIPEMD160(s interface{}) string {
byte_s := stringAndByte(s)
r := ripemd160.New()
r.Write(byte_s)
return fmt.Sprintf("%x", r.Sum(nil))
}

// MD5加密
func MD5(s interface{}) string {
byte_s := stringAndByte(s)
m := md5.New()
m.Write(byte_s)
return fmt.Sprintf("%x", m.Sum(nil))
}
18 changes: 18 additions & 0 deletions utils/rc4.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package utils

import (
"crypto/rc4"
)

// RC4加密
func RC4(data, key interface{}) []byte {
byte_data := stringAndByte(data)
byte_key := stringAndByte(key)
c, err := rc4.NewCipher(byte_key)
if err != nil {
panic(err)
}
plaintext := make([]byte, len(byte_data))
c.XORKeyStream(plaintext, byte_data)
return plaintext
}
47 changes: 47 additions & 0 deletions utils/sha.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package utils

import (
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
)

// SHA1加密
func SHA1(s interface{}) []byte {
byte_s := stringAndByte(s)
h := sha1.New()
h.Write(byte_s)
return h.Sum(nil)
}

// SHA224加密
func SHA224(s interface{}) []byte {
byte_s := stringAndByte(s)
h := sha256.New224()
h.Write(byte_s)
return h.Sum(nil)
}

// SHA256加密
func SHA256(s interface{}) []byte {
byte_s := stringAndByte(s)
h := sha256.New()
h.Write(byte_s)
return h.Sum(nil)
}

// SHA384加密
func SHA384(s interface{}) []byte {
byte_s := stringAndByte(s)
h := sha512.New384()
h.Write(byte_s)
return h.Sum(nil)
}

// SHA512加密
func SHA512(s interface{}) []byte {
byte_s := stringAndByte(s)
h := sha512.New()
h.Write(byte_s)
return h.Sum(nil)
}

0 comments on commit 859f7e5

Please sign in to comment.