Skip to content

Commit

Permalink
增加方法:Sha256、Sha512、HomePath、CreateMultiDir
Browse files Browse the repository at this point in the history
  • Loading branch information
ha666 committed May 13, 2021
1 parent 470b301 commit c1b2b17
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 3 deletions.
12 changes: 12 additions & 0 deletions encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ func Sha1(message []byte) []byte {
return h.Sum(nil)
}

func Sha256(bs []byte) []byte {
h := sha256.New()
h.Write(bs)
return h.Sum(nil)
}

func Sha512(bs []byte) []byte {
h := sha512.New()
h.Write(bs)
return h.Sum(nil)
}

func HmacMd5(message, secret []byte) []byte {
h := hmac.New(md5.New, secret)
h.Write(message)
Expand Down
79 changes: 79 additions & 0 deletions path.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package golibs

import (
"bytes"
"fmt"
"os"
"os/exec"
"os/user"
"runtime"
"strings"
)

// HomePath 获取当前用户根目录
func HomePath() (string, error) {
user, err := user.Current()
if nil == err {
return user.HomeDir, nil
}

// cross compile support
if "windows" == runtime.GOOS {
return homeWindows()
}

// Unix-like system, so just assume Unix
return homeUnix()
}

func homeUnix() (string, error) {
// First prefer the HOME environmental variable
if home := os.Getenv("HOME"); home != "" {
return home, nil
}
// If that fails, try the shell
var stdout bytes.Buffer
cmd := exec.Command("sh", "-c", "eval echo ~$USER")
cmd.Stdout = &stdout
if err := cmd.Run(); err != nil {
return "", err
}
result := strings.TrimSpace(stdout.String())
if result == "" {
return "", fmt.Errorf("blank output when reading home directory")
}
return result, nil
}

func homeWindows() (string, error) {
drive := os.Getenv("HOMEDRIVE")
path := os.Getenv("HOMEPATH")
home := drive + path
if drive == "" || path == "" {
home = os.Getenv("USERPROFILE")
}
if home == "" {
return "", fmt.Errorf("HOMEDRIVE, HOMEPATH, and USERPROFILE are blank")
}
return home, nil
}

//CreateMultiDir 调用os.MkdirAll递归创建文件夹
func CreateMultiDir(filePath string) error {
if pathIsExist(filePath) {
return nil
}
return os.MkdirAll(filePath, os.ModePerm)
}

// 判断所给路径文件/文件夹是否存在(返回true是存在)
func pathIsExist(path string) bool {
_, err := os.Stat(path)
if err != nil {
if os.IsExist(err) {
return true
}
return false
}
return true
}
8 changes: 6 additions & 2 deletions time.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import (

const (
Time_TIMEyyyyMMdd string = "20060102"
Time_TIMEyyyy_MM_dd string = "2006-01-02"
Time_TIMEStandard string = "2006-01-02 15:04:05"
Time_TIME_HH_mm_ss string = "15:04:05"
Time_TIMEMSSQL string = "2006-01-02T15:04:05.999Z"
Time_TIMEMSSQL string = "2006-01-02T15:04:05.000Z"
Time_TIMEMYSQL string = "2006-01-02T15:04:05+08:00"
Time_TIMEyyyyMMddHHmmss string = "20060102150405"
Time_TIMEyyyyMMddHHmmssffff string = "200601021504059999"
Time_TIMEJavaUtilDate string = "20060102150405000-0700"
Time_TIMEISO8601 string = "2006-01-02T15:04:05.999-0700"
Time_TIMEISO8601 string = "2006-01-02T15:04:05.000-0700"
)

// 获取当前日期
Expand Down Expand Up @@ -88,6 +89,9 @@ func Version2Time(version string) (t time.Time, err error) {
if !strings.Contains(version, ".") {
return t, errors.New("错误的版本号格式")
}
if strings.HasPrefix(version, "v") {
version = version[1:]
}
tmpVer := strings.Split(version, ".")
if len(tmpVer) < 3 {
return t, errors.New("错误的版本号格式")
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package golibs

const VERSION = "2020.712.900"
const VERSION = "2021.513.1742"

0 comments on commit c1b2b17

Please sign in to comment.