验证结果网址 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. 分布式配置中心:Spring Cloud Config

    最近在学习Spring Cloud的知识,现将分布式配置中心:Spring Cloud Config的相关知识笔记整理如下.[采用 oneNote格式排版]

  2. P5367 【模板】康托展开

    我们的生活充满了未知与玄学 ---------------------------------------- 链接:P5367 ------------------------------------ ...

  3. get请求与post请求中文乱码问题的解决办法

    首先出现中文乱码的原因是tomcat默认的编码方式是"ISO-8859-1",这种编码方式以单个字节作为一个字符,而汉字是以两个字节表示一个字符的. 一,get请求参数中文乱码的解 ...

  4. thinkphp论坛项目开发

    效果图 首先是数据库 /* Navicat MySQL Data Transfer Source Server : xm Source Server Version : 50553 Source Ho ...

  5. mui下拉上拉(明一)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  6. UVA1401 (字典树加简单dp)

    #pragma GCC optimize(2) #include <bits/stdc++.h> #define ll long long using namespace std; ; ; ...

  7. Android Studio 安装问题。

    安装时,这里要选Cancel 安装AS时因为选择了Setup Proxy, 后面带来很多问题. --------------------------------------------- 参考这个安装 ...

  8. Wannafly Winter Camp 2020 Day 5J Xor on Figures - 线性基,bitset

    有一个\(2^k\cdot 2^k\) 的全零矩阵 \(M\),给出 \(2^k\cdot 2^k\) 的 \(01\) 矩阵 \(F\),现在可以将 \(F\) 的左上角置于 \(M\) 的任一位置 ...

  9. 小白月赛22 D : 收集纸片

    D:收集纸片 考察点 : 全排列,对数据范围的估计程度 坑点 : 注意算最后回到初始的那步距离 析题得侃 : 一看题目最短路,诶呦,这不是最拿手的 BFS 走最短路吗?哈哈,定睛一看 这么多目的地,这 ...

  10. ORACLE不常用但实用的技巧- 树查询 level用法

    树查询 使用树查询的前提条件是: 在一条记录中记录了当前节点的ID和这个节点的父ID. 注意:一旦数据中出现了循环记录,如两个节点互为对方的父结点,系统就会报 ORA-01436错误(ORA-0143 ...