问题标签 [base62]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
22 回答
82125 浏览

python - Base 62 转换

您如何将整数转换为基数 62(如十六进制,但使用这些数字:'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')。

我一直在尝试为它找到一个好的 Python 库,但它们似乎都忙于转换字符串。Python base64 模块仅接受字符串并将单个数字转换为四个字符。我正在寻找类似于 URL 缩短器使用的东西。

0 投票
9 回答
13510 浏览

php - 将 URL 的 MD5 转换为 base62

我有一个脚本可以转换为 base 62 (A-Za-z0-9),但是如何从 MD5 中获取一个数字?

我在很多地方都读过,因为 MD5 中的数字大于 php 可以处理的整数,所以这将是不准确的......因为无论如何我想要一个短 URL 并且不打算使用整个哈希,可能只有 8 个字符其中……

所以我的问题是如何获得 MD5 哈希的部分数量?

另外,只使用部分 MD5 哈希是不是一个坏主意?

0 投票
6 回答
8500 浏览

php - PHP - 如何将 base_convert() 转换为基数 62

我需要一个base_convert()从 2 到 62 的函数,但我缺少我需要使用的数学,我知道由于 PHP 的限制,我需要使用 bcmath,这很好。

像这样的函数将一个数字从基数 10 转换为另一个基数,最高可达 62,但我想实现相同的功能base_convert(),例如:一个可以在任意基数之间转换的函数。

我找到了一个似乎可以做到这一点的函数,但它给我的感觉是有一些冗余和缓慢的代码,如果我知道德语,我想稍微调整一下,但我不知道。=(

这是该函数的可读性更高的版本:

谁能解释我上面的函数或者给我一些关于任意基数之间直接转换过程的说明?

0 投票
1 回答
1406 浏览

sql - 多基转换 - 使用 URL 缩短器的所有组合

我正在制作 URL 缩短器,并且正在努力将数字(id)编码为字符串的最佳方式。

我使用的是字符 0-9、AZ、az,所以它基本上是 base-62 编码。这是非常基本的,但它并没有使用所有可能的代码。它将产生的代码是:

请注意,没有使用代码 00 到 0z,对于 000 到 0zz 也是如此,依此类推。我想使用所有代码,如下所示:

它将是 base-62 和 base-63 的某种组合,根据位置不同具有不同的基数......使用 base-62 很容易,例如:

但我还没有设法从中进行多基转换,以利用所有代码。

0 投票
1 回答
1764 浏览

ruby-on-rails - 在 Rails 应用程序中混淆 id

我试图混淆所有离开服务器的 id,即出现在 URL 和 HTML 输出中的 id。

我编写了一个简单的 Base62 库,其中包含编码和解码方法。定义(或更好)覆盖 ActiveRecord 的 id 方法以返回 id 的编码版本,并调整控制器以加载解码后的资源params[:id]给我想要的结果。id 现在在 url 中采用 base62 编码,响应显示正确的资源。

现在我开始注意到通过has_many关系定义的子资源没有加载。例如,我有一个名为sUser的记录。has_many Post现在User.find(1).posts是空的,虽然有帖子user_id = 1。我的解释是 ActiveRecord 必须将user_idofPost与我已经覆盖的方法 id进行User比较,而不是与self[:id]. 所以基本上这使我的方法毫无用处。

我想要的是obfuscates_id在模型中定义类似的东西,其余的将被处理,即在适当的位置进行所有编码/解码并防止服务器返回 id。

是否有任何可用的宝石或有人暗示如何做到这一点?我敢打赌我不是第一个尝试这个的人。

0 投票
4 回答
154 浏览

jquery - jQuery 在不同情况下无法选择事物

注意:如果你“只是”一个 jQuery 开发人员,这篇文章中的某些内容可能看起来有点复杂(Base62 编码等)——实际上并非如此。尽管更多的技术细节与问题相关,但核心是 jQuery 不会选择带有大写字母的东西。谢谢!

嗨伙计!

所以我有一个由 Ajax 生成的列表。当您单击列表的标题时,会发送它的 ID,并且列表项会出现在它旁边。标准的东西。

由于我们使用的是 auto_increment ID,我们不希望用户知道数据库中有多少提交。因此,我将其编码为 Base62,然后再次解码。[请注意,这与问题无关,或者应该与问题无关]。

因此,当我的列表生成时,此代码将被输出。我们在 jQuery 旁边使用 CodeIgniter PHP - 这是在数据库结果的循环中。$this->basecrypt->encode()是一个简单的 CI 库,用于将整数(ID)转换为 Base62:

然后,在页面下方:

如您所见,这都是在同一个循环中生成的——查看输出的源代码显示,例如:

$('#title-1T').click[...]进而<div id="title-1T" [...]

所以,jQuery 应该没有任何问题,对吧?在我们开始对 ID 进行 Base62 处理之前,一切正常。我相信当 jQuery 包含大写字母时,它们不能/不会选择我们的 ID

如果我错了,请原谅我 - 相对而言,我对 jQuery 相当陌生 - 但为了测试我的观点,我将我的更改$this->basecrypt->encode()为 Base36。之前,它正在使用0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 之后,它正在使用0123456789abcdefghijklmnopqrstuvwxyz

With no capital letters, jQuery could select (and show the alert for testing purposes) just fine.

So what can I do? Is it safe for me to continue just using numbers and lowcase letters, in Base36 - and if so, what's the maximum integer size this can go up to? If not, what can I do about jQuery's problematic selection process?

Thanks!

Jack

EDIT: Included below is some example code from the page.

This is a part of the script returned in the file ajaxlist.php - it's called from Ajax and appears a couple of seconds after the page loads. I added in alert("clicked"); right near the beginning to see if that would appear - sadly, it doesn't... $(document).ready(function() {

Also returned from the same file, at the same time as the code above (just beneath it) is this:

If this helps anymore, let me know!


EDIT 2: ACTUAL OUTPUT RETURNED TO THE BROWSER.

This was taken from Firebug, and is the returned data (Ajax) to the browser:

Come on - you cannot say that shouldn't work... can you?!

0 投票
3 回答
3310 浏览

c++ - 在c ++中将md5字​​符串转换为base 62字符串

我正在尝试将 md5 字符串(base 16)转换为 c++ 中的 base 62 字符串。到目前为止,我发现的每个转换为 base 62 的解决方案只有在您可以将您的数字表示为 64 位整数或更小的整数时才有效。一个 md5 字符串是 128 位的,我一个人对此一无所获。

我应该只包含一个 bigint 库并完成它吗?

0 投票
4 回答
1610 浏览

php - 请解释这个base 62 PHP转换函数/算法

谁能解释下面的代码?那或指向我一些可以揭示一些信息的资源:)

它将整数转换为 base62 字符串。

更新:我的意思是问:谁能解释下面的算法?:) 谢谢。

0 投票
7 回答
28069 浏览

python - 在python中创建盐

如何在 python 中创建一个随机的 16 个字符的 base-62 salt?我需要它作为协议,但我不知道从哪里开始。谢谢。

0 投票
4 回答
1548 浏览

algorithm - Will this obfuscation algorithm for a URL shortener work?

DISCLAIMER: I am not asking how to make a URL shortener (I have already implemented the "bijective function" answer found HERE that uses a base-62 encoded string). Instead, I want to expand this implementation to obfuscate the generated string so that it is both:

A) not an easily guessable sequence, and

B) still bijective.

You can easily randomize your base-62 character set, but the problem is that it still increments like any other number in any other base. For example, one possible incremental progression might be {aX9fgE, aX9fg3, aX9fgf, aX9fgR, … ,}

I have come up with an obfuscation technique that I am pleased with in terms of requirement A), but I'm only partially sure that it satisfies B). The idea is this:

The only thing that is guaranteed to change in the incremental approach is the "1's place" (I'll use decimal terminology for practicality reasons). In the sample progression I gave earlier, that would be {E, 3, f, R, …}. So if each character in the base-62 set had its own unique offset number (say, its distance from the "zero character"), then you could apply the offset of the "1's place" character to the rest of the string.

For instance, let's assume a base-5 set with characters {A, f, 9, p, Z, 3} (in ascending order from 0 to 5). Each one would then have a unique offset of 0 to 5 respectively. Counting would look like {A, f, 9, p, Z, 3, fA, ff, f9, fp, …} and so on. So the algorithm, when given a value of fZ3p, would look at the p and, having an offset of +3, would permute the string into Zf9p (assuming the base-5 set is a circular array). The next incremental number would be fZ3Z, and with Z's offset being +4, the algorithm returns 39pZ. These permutated results would be handed off to the user as his/her "unique URL", who would never see the actual base-62 encoded string.

This approach certainly seems reversible; just look at the last character, and perform the same permutation with the negative offset. And I'm thinking that for this reason, it has to still be bijective. But I don't know if this is necessarily true? Are there any edge/corner cases I'm not considering?

EDIT : My intentions are more heavily weighed towards the length of the shortened-URL rather than the security of the pattern. I realize there are plenty of solutions involving cryptographic functions, block ciphers, etc. But I would like to emphasize that I am not asking the best way to achieve A), but rather, "is my offset-approach satisfying B)".

Any holes you can find would be appreciated.