All Projects → chanyipiaomiao → Hltool

chanyipiaomiao / Hltool

Licence: mit
Go 开发常用工具库, Google2步验证客户端,AES加密解密,RSA加密解密,钉钉机器人,邮件发送,JWT生成解析,Log,BoltDB操作,图片操作,json操作,struct序列化

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Projects that are alternatives of or similar to Hltool

Pyautolens
PyAutoLens: Open Source Strong Gravitational Lensing
Stars: ✭ 90 (-40.4%)
Mutual labels:  image-processing, image
Gift
Go Image Filtering Toolkit
Stars: ✭ 1,473 (+875.5%)
Mutual labels:  image-processing, image
Encryptor4j
Strong encryption for Java simplified
Stars: ✭ 92 (-39.07%)
Mutual labels:  aes, rsa
Processing Imageprocessing
Collection of basic image processing algorithms for processing
Stars: ✭ 87 (-42.38%)
Mutual labels:  image-processing, image
Gimage
A PHP library for easy image handling. 🖼
Stars: ✭ 148 (-1.99%)
Mutual labels:  image, jpg
Hybrid Crypto Js
RSA+AES hybrid encryption implementation for JavaScript. Works with Node.js, React Native and modern browsers.
Stars: ✭ 87 (-42.38%)
Mutual labels:  aes, rsa
Serverless Image Processor
AWS Lambda image processor
Stars: ✭ 106 (-29.8%)
Mutual labels:  image-processing, image
Encryptlab
A Free and Comprehensive Encrypt and Decrypt Tools Website with example code in Node.js, Website is looking for a new server.
Stars: ✭ 69 (-54.3%)
Mutual labels:  aes, rsa
Isketchnfill
Software that can autocomplete sketches as the user starts drawing.
Stars: ✭ 151 (+0%)
Mutual labels:  image-processing, image
Gil
Boost.GIL - Generic Image Library | Requires C++11 since Boost 1.68
Stars: ✭ 122 (-19.21%)
Mutual labels:  image-processing, image
Damselfly
Damselfly is a server-based Digital Asset Management system for photographs. The goal of Damselfly is to index an extremely large collection of images, and allow easy search and retrieval of those images, using metadata such as the IPTC keyword tags, as well as the folder and file names.
Stars: ✭ 86 (-43.05%)
Mutual labels:  image-processing, image
Lerc
Limited Error Raster Compression
Stars: ✭ 126 (-16.56%)
Mutual labels:  image-processing, image
Imageviewer
HDR, PFM, DDS, KTX, EXR, PNG, JPG, BMP image viewer and manipulator
Stars: ✭ 71 (-52.98%)
Mutual labels:  image-processing, jpg
Png To Ico
convert png to ico format
Stars: ✭ 88 (-41.72%)
Mutual labels:  image-processing, image
Skrop
Image transformation service using libvips, based on Skipper.
Stars: ✭ 71 (-52.98%)
Mutual labels:  image-processing, image
Pimg
📷 Mini Image Lazy Loader for P(R)eact and Vue
Stars: ✭ 97 (-35.76%)
Mutual labels:  image-processing, image
Low Latency Android Ios Linux Windows Tvos Macos Interactive Audio Platform
🇸Superpowered Audio, Networking and Cryptographics SDKs. High performance and cross platform on Android, iOS, macOS, tvOS, Linux, Windows and modern web browsers.
Stars: ✭ 1,121 (+642.38%)
Mutual labels:  aes, rsa
Pillow
The friendly PIL fork (Python Imaging Library)
Stars: ✭ 9,241 (+6019.87%)
Mutual labels:  image-processing, image
Aesthetics
Image Aesthetics Toolkit - includes Fisher Vector implementation, AVA (Image Aesthetic Visual Analysis) dataset and fast multi-threaded downloader
Stars: ✭ 113 (-25.17%)
Mutual labels:  image-processing, image
Bitmap
C++ Bitmap Library
Stars: ✭ 125 (-17.22%)
Mutual labels:  image-processing, image

hltool

Go 开发常用工具库

安装

使用golang官方 dep 管理依赖

go get github.com/chanyipiaomiao/hltool

功能列表

2步验证客户端

模拟Google Authenticator验证器命令行客户端

import (
    "github.com/chanyipiaomiao/hltool"
    "fmt"
)

func main() {
    totp := &hltool.TOTP{
        SecretKey: "xxxxxxxxxxx",
        Algorithm: "SHA1",
        Name: "HeHe",
    }
    n, t, err := hltool.TwoStepAuthGenNumber(totp)
    if err != nil {
        panic(err)
    }
    fmt.Printf("%s %s %d\n", totp.Name, n, t)
}

返回到目录

AES加密解密

package main

import (
	"encoding/base64"
	"fmt"
	"log"

	"github.com/chanyipiaomiao/hltool"
)

func main() {

	// AES 加解密 指定加密的密码
	goaes := hltool.NewGoAES([]byte("O8Hp8WQbFPT7b5AUsEMVLtIU3MVYOrt8"))

	// 加密数据
	encrypt, err := goaes.Encrypt([]byte("123456"))
	if err != nil {
		log.Fatalln(err)
	}

	fmt.Println(base64.StdEncoding.EncodeToString(encrypt))

	// 解密数据
	decrypt, err := goaes.Decrypt(encrypt)
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println(string(decrypt))

}

返回到目录

RSA加密解密

package main

import (
	"fmt"
	"log"
	"github.com/chanyipiaomiao/hltool"
)
func main() {

	// 生成 2048 位密钥对文件 指定名称
	err := hltool.NewRSAFile("id_rsa.pub", "id_rsa", 2048)
	if err != nil {
		log.Fatalln(err)
	}

	// 生成密钥对字符串
	// pub, pri, err := hltool.NewRSAString(2048)
	// if err != nil {
	// 	log.Fatalln(err)
	// }
	// fmt.Println(pub)
	// fmt.Println(pri)

	// 指定 公钥文件名 和 私钥文件名
	gorsa, err := hltool.NewGoRSA("id_rsa.pub", "id_rsa")
	if err != nil {
		log.Fatalln(err)
	}

	// 明文字符
	rawStr := "O8Hp8WQbFPT7b5AUsEMVLtIU3MVYOrt8"

	// 使用公钥加密
	encrypt, err := gorsa.PublicEncrypt([]byte(rawStr))
	if err != nil {
		log.Fatalln(err)
	}

	// 使用私钥解密
	decrypt, err := gorsa.PrivateDecrypt(encrypt)
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println(string(decrypt))
}

返回到目录

钉钉机器人通知

import (
	"log"
	"github.com/chanyipiaomiao/hltool"
)

dingtalk := hltool.NewDingTalkClient("钉钉机器URL", "消息内容", "text|markdown")
ok, err := hltool.SendMessage(dingtalk)
if err != nil {
	log.Fatalf("发送钉钉通知失败了: %s", err)
}

返回到目录

发送邮件

import (
	"log"
	"github.com/chanyipiaomiao/hltool"
)

username := "[email protected]"
host := "smtp.exmail.qq.com"
password := "password"
port := 465

subject := "主题"
content := "内容"
contentType := "text/plain|text/html"
attach := "附件路径" 或者 ""
to := []string{"[email protected]", "[email protected]"}
cc := []string{"[email protected]", "[email protected]"}

message := hltool.NewEmailMessage(username, subject, contentType, content, attach, to, cc)
email := hltool.NewEmailClient(host, username, password, port, message)
ok, err := hltool.SendMessage(email)
if err != nil {
	log.Fatalf("发送邮件失败了: %s", err)
}

返回到目录

JWT Token生成解析

import (
	"fmt"
	"log"

	"github.com/chanyipiaomiao/hltool"
)

func main() {

	// 签名字符串
	sign := "fDEtrkpbQbocVxYRLZrnkrXDWJzRZMfO"

	token := hltool.NewJWToken(sign)

	// -----------  生成jwt token -----------
	tokenString, err := token.GenJWToken(map[string]interface{}{
		"name": "root",
	})
	if err != nil {
		log.Fatalf("%s", err)
	}
	fmt.Println(tokenString)

	// -----------  解析 jwt token -----------
	r, err := token.ParseJWToken(tokenString)
	if err != nil {
		log.Fatalf("%s", err)
	}
	fmt.Println(r)

}

输出

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoicm9vdCJ9.NJMXxkzdBBWrNUO5u2oXFLU9FD18TWiXHqxM2msT1x0

map[name:root]

返回到目录

Log库

  • 支持按天分割日志
  • 不同级别输出到不同文件
  • 支持 文本/json日志类型,默认是json类型
  • 设置日志最大保留时间
import (
	"github.com/chanyipiaomiao/hltool"
)

func main() {
	
	commonFields := map[string]interface{}{
		"name": "zhangsan",
		"age":  "20",
	}

	hlog, _ := hltool.NewHLog("./test.log")
	// hlog.SetLevel("debug") debug|info|warn|error|fatal|panic
	logger, _ := hlog.GetLogger()

	// Info Warn 会输出到不同的文件
	logger.Info(commonFields, "测试Info消息")
	logger.Warn(commonFields, "测试Warn消息")
	
	// Error Fatal Panic 会输出到一个文件
	logger.Error(commonFields, "测试Error消息")
	logger.Fatal(commonFields, "测试Fatal消息")
	logger.Panic(commonFields, "测试Panic消息")
}

日志文件内容:

{"age":"20","level":"debug","msg":"测试Debug消息","name":"zhangsan","time":"2018-02-08 21:28:29"}
{"age":"20","level":"info","msg":"测试Info消息","name":"zhangsan","time":"2018-02-08 21:28:29"}
{"age":"20","level":"warning","msg":"测试Warn消息","name":"zhangsan","time":"2018-02-08 21:28:29"}
{"age":"20","level":"error","msg":"测试Error消息","name":"zhangsan","time":"2018-02-08 21:28:29"}

返回到目录

BoltDB嵌入式KV数据库

import (
	"log"

	"github.com/chanyipiaomiao/hltool"
)

func main() {

	// 数据库文件路径 表名
	db, err := hltool.NewBoltDB("./data/app.db", "token")
	if err != nil {
		log.Fatalf("%s", err)
	}
	db.Set(map[string][]byte{
		"hello": []byte("world"),
		"go":    []byte("golang"),
	})
	r, err := db.Get([]string{"hello", "go"})
	if err != nil {
		log.Fatalf("%s", err)
	}
	log.Println(r)
}

返回到目录

检测图片类型

package main

import (
	"fmt"

	"github.com/chanyipiaomiao/hltool"
)

func main() {

	bytes, _ := hltool.ImageToBytes("1.png")
	fmt.Println(hltool.ImageType(bytes))

}

输出结果:

image/png

返回到目录

图片转byte数组

package main

import (
	"fmt"

	"github.com/chanyipiaomiao/hltool"
)

func main() {

	bytes, err := hltool.ImageToBytes("1.png")
	if err != nil {
		fmt.Println(err)
	}

}

返回到目录

byte数组转换为png jpg

package main

import (
	"fmt"

	"github.com/chanyipiaomiao/hltool"
)

func main() {

	bytes, err := hltool.ImageToBytes("1.png")
	if err != nil {
		log.Fatalln(err)
	}

	err = hltool.BytesToImage(bytes, "111.png")
	if err != nil {
		log.Fatalln(err)
	}

}

返回到目录

json文件转换为byte数组

json文件内容

{
    "Name": "张三",
    "Age": 20,
    "Address": {
        "Country": "China",
        "Province": "Shanghai",
        "City": "Shanghai"
}
package main

import (
	"fmt"
	"log"

	"github.com/chanyipiaomiao/hltool"
)

func main() {

	// 读取json文件转换为 []byte
	b, err := hltool.JSONFileToBytes("/Users/helei/Desktop/test.json")
	if err != nil {
		log.Fatalln(err)
	}
}

返回到目录

json byte数组转换为 struct

package main

import (
	"fmt"
	"log"

	"github.com/chanyipiaomiao/hltool"
)

type Person struct {
	Name    string `json:"Name"`
	Age     int    `json:"Age"`
	Address struct {
		Country  string `json:"Country"`
		Province string `json:"Province"`
		City     string `json:"City"`
	} `json:"Address"`
}

func main() {

	// 读取json文件转换为 []byte
	b, err := hltool.JSONFileToBytes("/Users/helei/Desktop/test.json")
	if err != nil {
		log.Fatalln(err)
	}

	// json []byte转换为 struct
	p := new(Person)
	err = hltool.JSONBytesToStruct(b, p)
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println(p)
}

struct序列化成二进制文件和反序列化

二进制文件可以存储到磁盘上,再次利用

package main

import (
	"fmt"
	"log"

	"github.com/chanyipiaomiao/hltool"
)

// Person 人
type Person struct {
	Name    string 
	Age     int    
	Address struct {
		Country  string 
		Province string 
		City     string 
	} 
}

func main() {

	p := &Person{
		Name: "张三",
		Age:  20,
	}

	p.Address.Country = "China"
	p.Address.Province = "Shanghai"
	p.Address.City = "Shanghai"

	fmt.Println("序列化成二进制文件之前")
	fmt.Println(p)

	// 序列化成二级制文件,可以存储到磁盘上
	err := hltool.StructToBinFile(p, "/tmp/p.bin")
	if err != nil {
		log.Fatalln(err)
	}

	// 反序列化
	p2 := new(Person)
	err = hltool.BinFileToStruct("/tmp/p.bin", p2)
	if err != nil {
		log.Fatalln(err)
	}

	fmt.Println("从二进制文件中转换之后")
	fmt.Println(p2)

}

返回到目录

struct序列化成byte数组和反序列化

struct序列化成byte数组,可以存储到数据库中,再次利用

package main

import (
	"fmt"
	"log"

	"github.com/chanyipiaomiao/hltool"
)

// Person 人
type Person struct {
	Name    string `json:"Name"`
	Age     int    `json:"Age"`
	Address struct {
		Country  string `json:"Country"`
		Province string `json:"Province"`
		City     string `json:"City"`
	} `json:"Address"`
}

func main() {

	p := &Person{
		Name: "张三",
		Age:  20,
	}

	p.Address.Country = "China"
	p.Address.Province = "Shanghai"
	p.Address.City = "Shanghai"

	fmt.Println("struct序列化成[]byte")

	// struct序列化成[]byte,可以存储到数据库
	b, err := hltool.StructToBytes(p)
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println(p)
	fmt.Println(b)

	// []byte反序列化成struct 和序列化之前的结构体结构必须要一样
	fmt.Println("[]byte反序列化成struct")
	p2 := new(Person)
	err = hltool.BytesToStruct(b, p2)
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println(p2)

}

返回到目录

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].