前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >哈希竞猜游戏开发正式版丨哈希竞猜游戏系统开发成熟案例分析及源码

哈希竞猜游戏开发正式版丨哈希竞猜游戏系统开发成熟案例分析及源码

原创
作者头像
VC_MrsFu123
发布2022-07-04 17:33:42
2360
发布2022-07-04 17:33:42
举报

  什么是哈希函数?

  「哈希函数」H使用变长的数据分组M作为输入,生成固定长度的结果h=H(M)这一值也称为哈希值或者散列值。对于广义上的哈希函数而言,这个实际上就是把某个任意大小的集合映射到某个固定长度的集合里面,就好比我们编程语言经常会用到的哈希表,里面也是用到了哈希函数。

  哈希函数的特点:

  逆向运算困难;

  构造碰撞困难。

  如下特点:

  dist0与dist2,输出的内容一样,根据这个我们可以发现,主要输入内容一样,他们的哈希值就是一样的。

  dist0与dist1,我们只改变了一点点内容,发现输出的哈希值完全不一样,我们把这种情况,称之为“雪崩效应”。

  雪崩效应(Avalanche Effect)是密码学算法一个常见的特点,指的是输入数据的微小变换,就会导致输出数据的巨大变化。严格雪崩效应是雪崩效应的一个形式化指标,我们也常用来衡量均匀分布。严格雪崩效应指的是,如果输入数据的一位反转,输出数据的每一位都有50%的概率会发生变化。.

  So why develop blockchain standards?Because blockchain technology is of great significance for the future development of science and technology,but its rapid development and relevant systems do not match,which will lead to chaos in the blockchain field,which needs to be regulated and actively guided;Because we have suffered a lot in terms of intellectual property rights and standards,we should strive for international discourse and dominance in the blockchain field,which is conducive to maintaining the leading position in China's blockchain field;Because it is necessary to promote the application and implementation of blockchain technology,it is easier to reach a consensus in the field and promote the joint application and development of blockchain technology in various industries.

  type Hash func(data[]byte)uint32

  //hash一个hash算法,可以自定义hash算法,默认为crc32.ChecksumIEEE

  //replicas每个peer节点可产生几个虚拟节点,由该值确定

  //keys储存所有虚拟节点的hash值,每个值对应一个键值

  //hashMap根据hash值找到相应的peer,真实主机节点

  type Map struct{

  hash Hash

  replicas int

  keys[]int//Sorted

  hashMap map[int]string

  }

  //新建一个Map实例,用来存储节点和键值的关系

  func New(replicas int,fn Hash)*Map{

  m:=&Map{

  replicas:replicas,

  hash:fn,

  hashMap:make(map[int]string),

  }

  if m.hash==nil{

  m.hash=crc32.ChecksumIEEE

  }

  return m

  }

  //查看当前map中是否有键值

  func(m*Map)IsEmpty()bool{

  return len(m.keys)==0

  }

  //添加键值到map中

  func(m*Map)Add(keys...string){

  for _,key:=range keys{

  //一个key就是一个peer节点

  //对于每个key,都会生成replicas个虚拟节点,并把虚拟节点和key的对应关系存起来

  for i:=0;i<m.replicas;i++{

  //算法,将Node-A节点按照副本数量,分成0Node-A,1Node-A,2Node-A

  hash:=int(m.hash([]byte(strconv.Itoa(i)+key)))

  //算出的hash值添加到虚拟节点列表中

  m.keys=append(m.keys,hash)

  //将hash值和真实的节点进行关联

  m.hashMap[hash]=key

  }

  }

  //排序,这个动作非常重要,因为只有这样,才能构造一个有序的Hash环

  sort.Ints(m.keys)

  }

  //这边的key为一个cache中的key,即要查询的key

  //计算这个key的hash值,找到与之最为接近的虚拟节点,再通过虚拟节点找到具体的pee

  func(m*Map)Get(key string)string{

  if m.IsEmpty(){

  return""

  }

  //计算key对应的hash值

  hash:=int(m.hash([]byte(key)))

  //使用二分法查找key对应的hash值最近的虚拟节点

  idx:=sort.Search(len(m.keys),func(i int)bool{return m.keys<i>>=hash})

  //一致性hash的虚拟节点应该构成一个环

  if idx==len(m.keys){

  idx=0

  }

  return m.hashMap[m.keys[idx]]

  }

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
网站建设
网站建设(Website Design Service,WDS),是帮助您快速搭建企业网站的服务。通过自助模板建站工具及专业设计服务,无需了解代码技术,即可自由拖拽模块,可视化完成网站管理。全功能管理后台操作方便,一次更新,数据多端同步,省时省心。使用网站建设服务,您无需维持技术和设计师团队,即可快速实现网站上线,达到企业数字化转型的目的。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档