设置api密钥

by Sam Corcos

由Sam Corcos

我应该将我的API密钥设置多长时间? (How Long Should I Make My API Key?)

计算散列值的冲突概率 (Calculating collision probabilities of hashed values)

Say you built an API that generates public keys, and these keys all need to be unique and hard to guess. The most common way to do this is to use a hash function to generate a random series of numbers and letters. A typical hash looks something like the text below.

假设您构建了一个可生成公共密钥的API,那么这些密钥都必须是唯一的且难以猜测。 最常见的方法是使用哈希函数生成随机的数字和字母系列。 典型的哈希看起来像下面的文本。

AFGG2piXh0ht6dmXUxqv4nA1PU120r0yMAQhuc13i8

AFGG2piXh0ht6dmXUxqv4nA1PU120r0yMAQhuc13i8

A question that often comes up is, “How long does my hash need to be in order to ensure uniqueness?” Most people assume this is a difficult calculation. So they default to some very large number, like a 50-digit hash. The equation to approximate collision probability is actually quite simple.

经常出现的一个问题是:“为了确保唯一性,我的哈希需要多长时间?” 大多数人认为这是一个困难的计算。 因此,它们默认使用非常大的数字,例如50位哈希。 近似碰撞概率的方程实际上很简单。

我该如何计算? (How do I calculate?)

Let’s assume you’re using a good cryptographic algorithm (i.e. not JavaScript’s Math.random). Every language has a decent crypto package for generating random hashes. With Phoenix, you can use the Erlang :crypto package.

假设您使用的是好的加密算法(即不是JavaScript的Math.random )。 每种语言都有一个不错的加密软件包,用于生成随机哈希。 使用Phoenix,您可以使用Erlang :crypto包。

There are only two pieces of information you need to do the calculation:

您只需要执行两条信息即可进行计算:

  1. How many possible unique hash values can you create with the given inputs? We’ll assign this to the variable N.

    您可以使用给定的输入创建多少个唯一的哈希值? 我们将其分配给变量N。

  2. How many values could you possibly need to generate in the lifetime of your project? We’ll assign this to the variable k.

    在您的项目生命周期中,您可能需要生成多少个值? 我们将其分配给变量k。

To calculate the first value, add up all the possible characters that can go into your hash. Raise it to the power of the length of your hash.

要计算第一个值,请将所有可能包含在哈希中的字符加起来。 将其提高到哈希长度的力量。

So for example, if your hash value contains numbers, lowercase, and uppercase letters, that adds up to 62 total characters (10 + 26 + 26) that we can use. If we are generating a hash of only 3 characters in length, then:

因此,例如,如果您的哈希值包含数字,小写字母和大写字母,则总共可以使用62个字符(10 + 26 + 26)。 如果我们生成的长度仅为3个字符的哈希,则:

N = 62³ = 238,328 possible values

N =62³= 238,328可能的值

To calculate the second value, you need to think about what your app does and make some reasonable assumptions.

要计算第二个值,您需要考虑您的应用程序的功能并做出一些合理的假设。

Let’s say your app is generating a hash to assign to each of your customers. Let’s also say that your app is very niche. The absolute-best case scenario, your app will have 1000 customers over its lifetime. Then, for safety’s sake, we’ll multiply that by 10. We assume that you may need to generate 10,000 values over the course of your app’s life.

假设您的应用正在生成哈希以分配给每个客户。 我们还说您的应用程序非常利基。 在绝对最佳的情况下,您的应用在整个生命周期内将拥有1000个客户。 然后,为了安全起见,我们将其乘以10。我们假设您可能需要在应用的生命周期内生成10,000个值。

k = 10,000 upper bound for possible values that need to be created

k = 10,000需要创建的可能值的上限

So now we need to calculate. There are many algorithms we can use. We’ll use one of the simple approximations, where e is the mathematical constant (the base of the natural log), and k and N are the same values as above.

所以现在我们需要计算。 我们可以使用许多算法。 我们将使用一种简单的近似值 ,其中e是数学常数(自然对数的底数),而kN是与上述相同的值。

The base equation gives us the probability that all values are unique. Then we subtract that result from 1 to get the odds that you have a collision. If you don’t feel like writing your own equation, I’ve provided one below written in JavaScript.

基本方程式为我们提供了所有值都是唯一的概率。 然后,我们从1中减去该结果即可得出发生碰撞的几率。 如果您不想编写自己的方程式,请在下面提供用JavaScript编写的方程式。

So in the example above, calculate(N, k) yields a probability of approximately 100% that you will have a collision. So for that use case, you should use a hash of more than 3 characters in length.

因此,在上面的示例中, calculate(N,k)会产生大约100%的碰撞概率。 因此,对于该用例,应使用长度超过3个字符的哈希。

Now, if we were to take that same example but change the length of our hash from 3 to 5, we would get an N that is much larger (exponentials are good like that).

现在,如果我们采取同样的例子,但改变了我们从3哈希的长度为5,我们会得到一个N大得多 (指数都是这样的好)。

N = 62⁵ = ~900,000,000

N =62⁵=〜9亿

Assuming the same value k, our new probability of a collision is down to only 5.4%. What if we bumped that from 5 characters to 10?

假设值k相同,则我们发生碰撞的新概率仅为5.4% 。 如果我们将字符数从5个增加到10个怎么办?

N = 62¹⁰ = ~800,000,000,000,000,000

N =62¹⁰=〜800,000,000,000,000,000

Yes, that’s ~800 quintillion unique hashes. Which bring your odds of a collision down to 0.000000000062%. This is about a 1-in-50-billion chance that you have a conflict. And that’s with a hash of 10 digits — something like: BwQ1W6soXk.

是的,这是约800亿个独特的哈希值。 这使您的碰撞几率降至0.000000000062%。 这大约有500亿分之一的机会发生冲突。 这是10位数字的哈希-类似于:BwQ1W6soXk。

For another example, let’s say you’re a data processing company that deals with lots of transactions. We’ll say you deal with 1 million processes per second and you need to generate a hash for each of them. Let’s also say that you think this company could run for the next 100 years.

再举一个例子,假设您是一家处理大量交易的数据处理公司。 我们将说您每秒处理一百万个进程,并且需要为每个进程生成一个哈希。 假设您认为这家公司可以在未来100年经营。

k = ~3,000,000,000,000,000

k = 〜3,000,000,000,000,000

That comes out to about 3 quadrillion hashes that you need made that all need to be unique. That’s a lot of data! But even with this extremely large number to work with, you only need a 21-digit hash to ensure a 1-in-10-million chance of collision over the lifetime of the project.

这样就得出了大约3个需要满足所有要求的唯一性的四方哈希。 大量的数据! 但是,即使可以处理的数目如此之大,您也只需要使用21位数字的哈希值,就可以确保在项目生命周期中发生碰撞的可能性达到了十分之一。

So the next time you’re worried about the length of your hash in ensuring uniqueness, know that you don’t need one as long as you think you do. Plus, you can do the calculation yourself without much effort.

因此,下次您担心确保唯一性时哈希值的长度时,请知道您不需要的时间就与您认为的一样长。 另外,您可以自己进行计算,而无需花费太多精力。

Sam Corcos is the lead developer and co-founder of Sightline Maps, the most intuitive platform for 3D printing topographical maps, as well as LearnPhoenix.io, an intermediate-advanced tutorial site for building scalable production apps with Phoenix and React.

Sam Corcos是Sightline Maps (用于3D打印地形图的最直观的平台)以及LearnPhoenix.io (用于使用Phoenix和React构建可扩展的生产应用程序的中级高级教程网站)的首席开发人员和共同创始人。

翻译自: https://www.freecodecamp.org/news/how-long-should-i-make-my-api-key-833ebf2dc26f/

设置api密钥

设置api密钥_我应该将我的API密钥设置多长时间?相关推荐

  1. 我的世界设置java路径_我的世界启动器Java路径怎么设置?java路径设置攻略

    我的世界这款游戏在电脑上打开是需要用到启动器的,不过很多玩家会经常碰到java路径设置不正确等等情况,那么我的世界启动器Java路径怎么设置呢?下面巴士小编就给大家带来java路径设置攻略. 我的世界 ...

  2. 安卓投屏大师_安卓免费录屏软件哪个好用 免费长时间录屏软件推荐

    安卓免费录屏软件哪个好用,安卓市场上的录屏软件也是比较的多,而且很多都是收费,有很多的小伙伴们对于这个录屏也是有着一定的需求,对此也是想要知道安卓免费录屏软件哪个好用有哪些,下面就一起来看看吧. 安卓 ...

  3. python rest api 测试_如何用Python编写REST API的单元测试

    在过去的几个月中,正在从事一个名为B的项目.它是带有简单Web UI的徽章生成器,用于添加数据并生成PDF可打印徽章.B后端现在已转移到REST-API并测试REST-API中使用的功能,我们需要一些 ...

  4. excel怎么设置打印区域_别再浪费打印纸了!这样设置,Excel表格再大都能打印成一页!...

    在打印Excel表格时,你是不是经常碰到过这种情况: 明明排版好的表格,结果打印完却发现只显示一半...... 表格太宽导致无法打印在A4纸上...... 今天叨叨君就来分享几个有效的解决方法,教你轻 ...

  5. arnold如何设置鱼眼相机_华为相机实用技巧——如何调整相机设置?(2)

    上一篇提到了,相机的基础功能,这一篇来讲一下相机的基础设置,如何调整相机的设置,让相机更高效率的工作. 打开 相机,点击 设置,进入相机设置界面,您可以: 调整相机分辨率:在相机设置界面,点击分辨率, ...

  6. html设置页面大小_如何将Word文档页面大小设置为16开?

    某大学毕业生在对其毕业论文进行排版时,要求将纸张大小设置为16开,但Word默认的纸张大小为A4纸,如何将页面大小设置为16开? 1解决方案 通过设置"纸张大小"功能,将其页面设置 ...

  7. c# 无法检索解密密钥_使用C#检索Windows产品密钥

    c# 无法检索解密密钥 注意:今天早上在我的博客上发布了此内容,但也感到字节社区可以从中受益. 我已经在不同的论坛上多次问过这个问题,并认为我会开发一种用于检索Windows产品密钥的解决方案. 实际 ...

  8. 华为手机设置页面黑色_华为手机微信猫和老鼠主题皮肤怎么设置 ?超个性主题等你来设置!...

    阅读本文前,请您先点击上面的蓝色字体,再点击"关注",这样您就可以继续免费收到文章了.每天都有分享,完全是免费订阅,请放心关注. 注:本文转载自网络,如有侵权,请联系删除谢谢. 微 ...

  9. vue设置右边距_那些PPT高手,都是怎样设置软件的

    文/诺灰 一周进步签约作者 关注一周进步的朋友,相信绝大多数是对 PPT 的制作比较感兴趣的.不论你是刚接触 PPT 的大学新生,还是已经接触 PPT 数个年头的上班族,在一周进步这里,你总能得到更为 ...

最新文章

  1. MIT新开发的 AI 模型有望改进恶性脑瘤治疗
  2. Ansible自动化运维工具介绍
  3. 开源配置管理平台-Apollo
  4. cpp加密php,常用的数据加密规则算法(php包含MD5和RSA)
  5. JavaScript 字典类
  6. 为什么Note.js 里面没有sap.ui.core.Control.extend()这样的代码,但是runtime有自动生成有
  7. 【编译原理】语言的定义
  8. java文件读入原理_描述一下JVM加载class文件的原理机制
  9. python 常用模块函数_python函数和常用模块(三),Day5
  10. Windows下的开发环境
  11. php案例之后台数据显示-- mysqli面向过程版(procedure oriented programming = POP)
  12. vscode remote 离线环境搭建
  13. 【STM32训练—SIM900A模块】第二篇、STM32驱动SIM900A发送中文和英文短信
  14. Revisiting Stereo Depth Estimation From a Sequence-to Sequence Perspective with Transformer——阅读阶段
  15. 辛东方重组百花园传媒 人员大调整
  16. 大魔王程序员生成记#06.2#C语言习题
  17. 高德足迹点Android,高德地图怎么点亮城市 足迹地图查看方法
  18. B数和B+树、红黑树数据结构的特点
  19. Flatten()详解
  20. 谷粒商城异步编排(三十二)

热门文章

  1. python-万能函数装饰器
  2. 用Canvas为网页加入动态背景
  3. java基础----数据类型转化
  4. SwarmKit知多少——来自源码世界的深入解读
  5. 无法推动项目起步?试试麦当劳理论
  6. ajax mvc3 razor 分页
  7. Unicode汉字编码
  8. Mobile and MID opportunities in APAC
  9. 2018年12月份GitHub上最热门的Java开源项目
  10. Oracle备份与恢复 关于expdp导出远程数据到本地的使用