验证结果网址 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语言 实现哈希算法的更多相关文章

  1. R语言实现︱局部敏感哈希算法(LSH)解决文本机械相似性的问题(二,textreuse介绍)

    每每以为攀得众山小,可.每每又切实来到起点,大牛们,缓缓脚步来俺笔记葩分享一下吧,please~ --------------------------- 上一篇(R语言实现︱局部敏感哈希算法(LSH) ...

  2. 一致性哈希算法——算法解决的核心问题是当slot数发生变化时,能够尽量少的移动数据

    一致性哈希算法 摘自:http://blog.codinglabs.org/articles/consistent-hashing.html 算法简述 一致性哈希算法(Consistent Hashi ...

  3. Iconfinder 如何杜绝盗版,哈希算法检测图像重复

    原地址:http://blog.jobbole.com/65914/ 本文由 伯乐在线 - 小鱼 翻译自 Silviu Tantos.欢迎加入技术翻译小组.转载请参见文章末尾处的要求. [伯乐在线导读 ...

  4. _00013 一致性哈希算法 Consistent Hashing 新的讨论,并出现相应的解决

    笔者博文:妳那伊抹微笑 博客地址:http://blog.csdn.net/u012185296 个性签名:世界上最遥远的距离不是天涯,也不是海角,而是我站在妳的面前.妳却感觉不到我的存在 技术方向: ...

  5. os常用模块,json,pickle,shelve模块,正则表达式(实现运算符分离),logging模块,配置模块,路径叠加,哈希算法

    一.os常用模块 显示当前工作目录 print(os.getcwd()) 返回上一层目录 os.chdir("..") 创建文件包 os.makedirs('python2/bin ...

  6. 字符串哈希算法(以ELFHash详解)

    更多字符串哈希算法请参考:http://blog.csdn.net/AlburtHoffman/article/details/19641123 先来了解一下何为哈希: 哈希表是根据设定的哈希函数H( ...

  7. 大数据技术之_16_Scala学习_13_Scala语言的数据结构和算法_Scala学习之旅收官之作

    第十九章 Scala语言的数据结构和算法19.1 数据结构(算法)的介绍19.2 看几个实际编程中遇到的问题19.2.1 一个五子棋程序19.2.2 约瑟夫问题(丢手帕问题)19.2.3 其它常见算法 ...

  8. ELFhash - 优秀的字符串哈希算法

    ELFhash - 优秀的字符串哈希算法 2016年10月29日 22:12:37 阅读数:6440更多 个人分类: 算法杂论算法精讲数据结构 所属专栏: 算法与数据结构   版权声明:本文为博主原创 ...

  9. 一致哈希算法Java实现

    一致哈希算法(Consistent Hashing Algorithms)是一个分布式系统中经常使用的算法. 传统的Hash算法当槽位(Slot)增减时,面临全部数据又一次部署的问题.而一致哈希算法确 ...

随机推荐

  1. ggEditor流程图增加网格背景

    参考官方文档: https://www.yuque.com/antv/g6/plugin.tool.grid react-ggEditor如何使用 import { Flow } from 'gg-e ...

  2. 牛客网剑指offer第19题——顺时针打印矩阵

    这个题看似很简单: 题目: 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 ...

  3. Spring boot2.0学习笔记(一)

    学习环境: jdk1.8 (Spring Boot 推荐jdk1.8及以上): java version "1.8.0_241" Maven 3.x (maven 3.2 以上版本 ...

  4. 通过sd文件发布的FeatureAccess服务不能查看到图层

    发布服务有两种方法, 1. 用ArcMap --Share As - service --publish a service 此方法可以直接将地图数据发布到ArcGIS  Server 的地图服务中, ...

  5. SpringBoot整合持久层技术--(二)MyBatis

    简介: 原名iBatis,SpringBoot中使用MyBatis: pom.xml <dependency> <groupId>org.springframework.boo ...

  6. 《趣谈 Linux 操作系统》学习笔记(二):对 Linux 操作系统的理解

    首先,我们知道操作系统是管理和控制计算机硬件与软件资源的计算机程序.这里把操作系统想象为一个软件外包公司,其内核就相当于这家外包公司的老板,那么我们可以把自己的角色切换成这家外包公司的老板,设身处地的 ...

  7. [PAT] A1017 Queueing at Bank

    [思路] 1:将所有满足条件的(到来时间点在17点之前的)客户放入结构体中,结构体的长度就是需要服务的客户的个数.结构体按照到达时间排序. 2:wend数组表示某个窗口的结束时间,一开始所有窗口的值都 ...

  8. 【Python】七段数码管绘制问题

    问题分析: 绘制路径: 代码: import turtle #引入绘图库turtle def drawLine(draw): #绘制单段数码管 turtle.pendown() if draw els ...

  9. PHP错误日志文件Warning:PHP Startup: Unable to load dynamic library...

    由于我的环境是通过源码编译安装的,安装的时候配置信息和一些其他扩展没安装或设置好: php.err文件一直有这些提示,虽然不影响服务启动,但是看着心好累啊,决定要消灭他们. 问题描述: 出现原因: 上 ...

  10. C# 一次循环获取树的两种方法

    第一种方法好些 第二种方法如果中间断开就会成为一级 private static List<Menu> MenuTree() { , ParentId = , Name = "a ...