最短的哈希? MD5 / SHA。首字母,混帐 [英] Shortest hash? MD5 / SHA . First chars, git

查看:163
本文介绍了最短的哈希? MD5 / SHA。首字母,混帐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要散列函数。用户将把这些散列写入计算机,所以散列应该很短。
我将在数据库中存储大约50000000条记录。每个必须有自己的散列。我想要有独特的哈希。但是如果有一些记录有相同的散列,我可以接受。唯一更好。

MD2对我来说很安全,但hash很长:8350e5a3e24c153df2275c9f80692773 - 32个字符。如果你必须在keybord上写10个MD2哈希值,你不会很高兴......

Git为每个提交(40个字符)使用SHA1。但在输出中只显示前7个字符:

  $ git log 
commit e2cfc89fae5b43594b2c649fd4c05bcc6d2d12ac
...
承诺56a8b4c50d4269dc3f88727472933fd81231f63b
...
犯ce2e9ddbe896b9592abbd5fcb6604b181809d523
...
犯498c49833516ea33b6a40697634ea6e3cfd62328
...
犯b7d78aea415e64d8d441f9747fe6d5d48fe54ee5

$ git log --oneline |头-n 5
e2cfc89 commnit消息...
56a8b4c commnit消息...
ce2e9dd commnit消息...
498c498 commnit消息...
b7d78ae commnit消息...

安全性/独特性如何?例如,如果我将使用MD5 / SHA-1 / SHA-256中的前5个或10个字符,它足够安全吗?



谢谢。

解决方案

查看 hashids ,它旨在通过主键(或其他一些唯一编号)生成独特的YouTube风格哈希。它不是真正的散列,因为MD5和SHA-1的设计是可逆的。



例如,如果您想散列您的单个整数主键,您可能会得到像

 (PK:1)=< (hashid:8dY0qQ)

这是从您控制的秘密值开始播种的,因此用户无法确定他们真正参考的主键。如果您的数据库涉及更多,比如说多个分片和复杂键,那么您仍然可以。 hashids 以整数列表作为输入:

 (3,171 ,24)= (243j7Z)

作为开发者,您负责定义哈希的 minimum 长度。随着您生成越来越多的哈希, hashids 可能会生成稍长的哈希值。



哈希值是唯一的给定的输入(初始种子,最小散列长度和散列整数列表):

lockquote

没有冲突 STRONG>。您生成的哈希值应该是唯一的。


支持


  • JavaScript

  • Ruby

  • Python

  • Java
  • li>
  • Perl

  • CoffeeScript

  • Objective-C
  • li>
  • Lua

  • Node.js

  • .NET


I need hash function. Users will be write these hash to computer so hash should be short. I will have about 50 000 000 records in database. Each must have own hash. I would like to have unique hashes. But if a little records have the same hash, I can accept. Unique is better.

MD2 is good secure for me, but hash is long: "8350e5a3e24c153df2275c9f80692773" - 32 chars. If you must write 10 MD2 hash on keybord you do not happy...

Git use SHA1 for each commit (40 chars). But in output show only first 7 chars:

$ git log
commit e2cfc89fae5b43594b2c649fd4c05bcc6d2d12ac
...
commit 56a8b4c50d4269dc3f88727472933fd81231f63b
...
commit ce2e9ddbe896b9592abbd5fcb6604b181809d523
...
commit 498c49833516ea33b6a40697634ea6e3cfd62328
...
commit b7d78aea415e64d8d441f9747fe6d5d48fe54ee5

$ git log --oneline | head -n 5
e2cfc89 commnit message...
56a8b4c commnit message...
ce2e9dd commnit message...
498c498 commnit message...
b7d78ae commnit message...

How is it secure/unique? If I will use for example first 5 or 10 chars from MD5/SHA-1/SHA-256 is it enough secure?

Thank you.

解决方案

Check out hashids, which is designed to generate unique YouTube-style hashes from your primary keys (or some other set of unique numbers). It's not really a hash in the sense that MD5 and SHA-1 are, as it's designed to be reversible.

As an example, if you want to "hash" your single integer primary key, you may get a relationship like

(PK: 1) <=> (hashid: 8dY0qQ)

This is seeded from a secret value that you control, so users are not able to determine the primary key they're really referencing. If your database is somewhat more involved, say with multiple shards and complex keys, you're still okay. hashids takes a list of integers as input:

(3, 171, 24) <=> (243j7Z)

As the developer, you are responsible for defining the hash's minimum length. As you generate more and more hashes, hashids may generate slightly longer hashes.

Hashes are guaranteed to be unique for a given input (initial seed, minimum hash length, and list of integers to hash):

There are no collisions. Your generated hashes should be unique.

There is support for

  • JavaScript
  • Ruby
  • Python
  • Java
  • PHP
  • Perl
  • CoffeeScript
  • Objective-C
  • Go
  • Lua
  • Node.js
  • .NET

这篇关于最短的哈希? MD5 / SHA。首字母,混帐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆