go语言 实现哈希算法
验证结果网址 http://www.fileformat.info/tool/hash.htm "golang.org/x/crypto/md4"不存在时,解决方法:
cd $GOPATH/src
mkdir -p golang.org/x/
cd golang.org/x/
git clone https://github.com/golang/crypto.git 实现md4加密算法
package main import (
"encoding/hex"
"fmt"
"hash" "golang.org/x/crypto/md4"
) func main() {
res := MD4("123456")
fmt.Println(res)
} // MD4 MD4
func MD4(text string) string {
var hashInstance hash.Hash
hashInstance = md4.New()
arr, _ := hex.DecodeString(text)
hashInstance.Write(arr)
bytes := hashInstance.Sum(nil)
return fmt.Sprintf("%x", bytes) } 实现封装哈希加密算法
package main import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"fmt"
"hash" "golang.org/x/crypto/md4"
"golang.org/x/crypto/ripemd160"
) func main() {
res := HASH("123456", "sha256", true)
fmt.Println(res)
} // HASH HASH
func HASH(text string, hashType string, isHex bool) string {
var hashInstance hash.Hash
switch hashType {
case "md4":
hashInstance = md4.New()
case "md5":
hashInstance = md5.New()
case "sha1":
hashInstance = sha1.New()
case "sha256":
hashInstance = sha256.New()
case "sha512":
hashInstance = sha512.New()
case "ripemd160":
hashInstance = ripemd160.New()
}
if isHex {
arr, _ := hex.DecodeString(text)
hashInstance.Write(arr)
} else {
hashInstance.Write([]byte(text))
} bytes := hashInstance.Sum(nil)
return fmt.Sprintf("%x", bytes)
} // MD4 MD4
func MD4(text string, isHex bool) string {
var hashInstance hash.Hash
hashInstance = md4.New()
if isHex {
arr, _ := hex.DecodeString(text)
fmt.Println(arr)
hashInstance.Write(arr)
} else {
hashInstance.Write([]byte(text))
} bytes := hashInstance.Sum(nil)
return fmt.Sprintf("%x", bytes)
} // MD5 MD5
func MD5(text string, isHex bool) string {
var hashInstance hash.Hash
hashInstance = md5.New()
if isHex {
arr, _ := hex.DecodeString(text)
fmt.Println(arr)
hashInstance.Write(arr)
} else {
hashInstance.Write([]byte(text))
} bytes := hashInstance.Sum(nil)
return fmt.Sprintf("%x", bytes)
} 实现双哈希算法
func sha256Double(text string, isHex bool) []byte {
hashInstance := sha256.New()
if isHex {
arr, _ := hex.DecodeString(text)
hashInstance.Write(arr)
} else {
hashInstance.Write([]byte(text))
}
bytes := hashInstance.Sum(nil)
hashInstance.Reset()
hashInstance.Write(bytes)
bytes = hashInstance.Sum(nil)
return bytes
}
func sha256DoubleString(text string, isHex bool) string {
bytes := sha256Double(text, isHex)
return fmt.Sprintf("%x", bytes)
}
go语言 实现哈希算法的更多相关文章
- R语言实现︱局部敏感哈希算法(LSH)解决文本机械相似性的问题(二,textreuse介绍)
每每以为攀得众山小,可.每每又切实来到起点,大牛们,缓缓脚步来俺笔记葩分享一下吧,please~ --------------------------- 上一篇(R语言实现︱局部敏感哈希算法(LSH) ...
- 一致性哈希算法——算法解决的核心问题是当slot数发生变化时,能够尽量少的移动数据
一致性哈希算法 摘自:http://blog.codinglabs.org/articles/consistent-hashing.html 算法简述 一致性哈希算法(Consistent Hashi ...
- Iconfinder 如何杜绝盗版,哈希算法检测图像重复
原地址:http://blog.jobbole.com/65914/ 本文由 伯乐在线 - 小鱼 翻译自 Silviu Tantos.欢迎加入技术翻译小组.转载请参见文章末尾处的要求. [伯乐在线导读 ...
- _00013 一致性哈希算法 Consistent Hashing 新的讨论,并出现相应的解决
笔者博文:妳那伊抹微笑 博客地址:http://blog.csdn.net/u012185296 个性签名:世界上最遥远的距离不是天涯,也不是海角,而是我站在妳的面前.妳却感觉不到我的存在 技术方向: ...
- os常用模块,json,pickle,shelve模块,正则表达式(实现运算符分离),logging模块,配置模块,路径叠加,哈希算法
一.os常用模块 显示当前工作目录 print(os.getcwd()) 返回上一层目录 os.chdir("..") 创建文件包 os.makedirs('python2/bin ...
- 字符串哈希算法(以ELFHash详解)
更多字符串哈希算法请参考:http://blog.csdn.net/AlburtHoffman/article/details/19641123 先来了解一下何为哈希: 哈希表是根据设定的哈希函数H( ...
- 大数据技术之_16_Scala学习_13_Scala语言的数据结构和算法_Scala学习之旅收官之作
第十九章 Scala语言的数据结构和算法19.1 数据结构(算法)的介绍19.2 看几个实际编程中遇到的问题19.2.1 一个五子棋程序19.2.2 约瑟夫问题(丢手帕问题)19.2.3 其它常见算法 ...
- ELFhash - 优秀的字符串哈希算法
ELFhash - 优秀的字符串哈希算法 2016年10月29日 22:12:37 阅读数:6440更多 个人分类: 算法杂论算法精讲数据结构 所属专栏: 算法与数据结构 版权声明:本文为博主原创 ...
- 一致哈希算法Java实现
一致哈希算法(Consistent Hashing Algorithms)是一个分布式系统中经常使用的算法. 传统的Hash算法当槽位(Slot)增减时,面临全部数据又一次部署的问题.而一致哈希算法确 ...
随机推荐
- Qt编写的项目作品1-自定义控件大全
一.功能特点 超过160个精美控件,涵盖了各种仪表盘.进度条.进度球.指南针.曲线图.标尺.温度计.导航条.导航栏,flatui.高亮按钮.滑动选择器.农历等.远超qwt集成的控件数量. 每个类都可以 ...
- [Contract] public、external, private、internal 在 Solidity 中如何选择
合约内部访问的用 private,修饰内部变量时选择.通过 external 函数返回. 合约内部.外部均可访问的用 public. 仅在合约外部访问的用 external,明确暴露给前端使用时选择. ...
- SpringBoot从1.5.1→2.2.4项目加包扫雷一:Error:(8, 44) java: 程序包org.springframework.boot.web.support不存在
更换成新包即可import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
- phpcms抛出的二维数组转移到js,js中for....in遍历数组,用“.”连接来读出一维数组值
直切正题: 1.phpcms在模版中读出数组有很多中方法,如,{pc:content action="lists"}或{pc:get sql=""},经过{lo ...
- Ubuntu中FTP安装配置及基本概念(原创)
注:本文出自博主 Chloneda:个人博客 | 博客园 | Github | Gitee | 知乎 本文源链接:https://www.cnblogs.com/chloneda/p/ftp-inst ...
- HDU1163 - Eddy's digital Roots
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1163 九余数:一个数除于9所得到的余数,即模9得到的值 求九余数: 求出一个数的各位数字之和,如果是两 ...
- thinkphp论坛项目开发
效果图 首先是数据库 /* Navicat MySQL Data Transfer Source Server : xm Source Server Version : 50553 Source Ho ...
- JMeter-显示调试日志log
JMeter-调试日志记录 参考文档:https://jmeter.apache.org/usermanual/hints_and_tips.html 大多数测试元素包括调试日志记录. 如果从GUI运 ...
- Tomcat配置绝对路径
JSP访问项目工程下的地址 <video width="640" height="264" controls src="http://local ...
- 内网IPC$入侵加pstools之远程控制
前言: IPC$(Internet process connection)是指内网里面的文件共享连接,通常很多机子的默认共享都是打开的,在cmd下使用命令net share可查看自己的IPC$是否打开 ...