Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
146 lines (123 sloc) 3.07 KB
package utils
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"hash"
"io"
"os"
)
/* define strcut for md5, sha1 hash... */
type md5Hash struct{}
type sha1Hash struct{}
type sha256Hash struct{}
/* caculate the hash value of text string */
func stringHash(h hash.Hash, origin string) string {
h.Write([]byte(origin))
return hex.EncodeToString(h.Sum(nil))
}
/* caculate the hash value of file */
func fileHash(h hash.Hash, filepath string) string {
file, err := os.Open(filepath)
if err != nil {
return ""
}
defer file.Close()
_, err = io.Copy(h, file)
if err != nil {
return ""
}
return hex.EncodeToString(h.Sum(nil))
}
/* caculate the hash value of text string and encode to url bas64 */
func stringHashToBase64(h hash.Hash, origin string) string {
h.Write([]byte(origin))
return base64.URLEncoding.EncodeToString(h.Sum(nil))
}
/* caculate the hash value of file and encode to url bas64 */
func fileHashToBase64(h hash.Hash, filepath string) string {
file, err := os.Open(filepath)
if err != nil {
return ""
}
defer file.Close()
_, err = io.Copy(h, file)
if err != nil {
return ""
}
return base64.URLEncoding.EncodeToString(h.Sum(nil))
}
/**
* Caculate md5 hash for string text.
*/
func (hash *md5Hash) String(origin string) string {
return stringHash(md5.New(), origin)
}
/**
* Caculate md5 hash for file.
*/
func (hash *md5Hash) File(filepath string) string {
return fileHash(md5.New(), filepath)
}
/**
* Caculate md5 hash for string text and encode to url base64.
*/
func (hash *md5Hash) StringToBase64(origin string) string {
return stringHashToBase64(md5.New(), origin)
}
/**
* Caculate md5 hash for file and encode to url base64.
*/
func (hash *md5Hash) FileToBase64(filepath string) string {
return fileHashToBase64(md5.New(), filepath)
}
/**
* Caculate sha1 hash for string text.
*/
func (hash *sha1Hash) String(origin string) string {
return stringHash(sha1.New(), origin)
}
/**
* Caculate sha1 hash for file.
*/
func (hash *sha1Hash) File(filepath string) string {
return fileHash(sha1.New(), filepath)
}
/**
* Caculate sha1 hash for string text and encode to url base64.
*/
func (hash *sha1Hash) StringToBase64(origin string) string {
return stringHashToBase64(sha1.New(), origin)
}
/**
* Caculate sha1 hash for file and encode to url base64.
*/
func (hash *sha1Hash) FileToBase64(filepath string) string {
return fileHashToBase64(sha1.New(), filepath)
}
/**
* Caculate sha256 hash for string text.
*/
func (hash *sha256Hash) String(origin string) string {
return stringHash(sha256.New(), origin)
}
/**
* Caculate sha256 hash for file.
*/
func (hash *sha256Hash) File(filepath string) string {
return fileHash(sha256.New(), filepath)
}
/**
* Caculate sha256 hash for string text and encode to url base64.
*/
func (hash *sha256Hash) StringToBase64(origin string) string {
return stringHashToBase64(sha256.New(), origin)
}
/**
* Caculate sha256 hash for file and encode to url base64.
*/
func (hash *sha256Hash) FileToBase64(filepath string) string {
return fileHashToBase64(sha256.New(), filepath)
}