字符串加密-生成独特的模式,例如Spotify代码 [英] String encryption - generate unique pattern like Spotify codes

查看:215
本文介绍了字符串加密-生成独特的模式,例如Spotify代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天我已经阅读了问题创建Costum模板的算法/来自字符串的代码。由于问题的表述方式不尽人意,因此立即被否决了。但是,我认为问题本身还不错,所以我决定再次提出一个希望更好的版本。

Yesterday I've read the question Algorithm to create costum Template/Code from String. Because the question was not formulated that well it was downvoted just instantly. However, the question itself was in my opinion not that bad, so I decided to ask a hopefully better version of this question again.

好,我想知道字符串加密的方式如何例如新的Spotify代码正在运行。参见下图:

Ok, Im wondering how's the string encryption e.g. of the new Spotify codes is working. See the image below:


我对在何种程度上可以在javascript中实现这种模式加密非常感兴趣。

I would be super interested in the extent to which it is possible to implement something like this pattern-encryption in javascript.

Spotify代码-我已经在上面提到过 -排成一行,分成不同大小的条形。

The Spotify codes - I've already mentioned above - are structured in a row that is divided into different sized bars.

因此,假设有一行被分成24个小节,并且所有小节的大小都可以为'3','5','7'或'9'

So let's say there is a row that is divided into 24 bars and all of the bars can have the size '3', '5', '7' or '9'.

 string = 'hello'   -->  pattern = '3,3,5,7,9,3,7,9,9,3,3,5,3,9,5,3,3,7,5,9,3,9,3,9'



什么是翻译字符串的好方法/简单方法(让我们说5个字符)变成一个独特的模式,那之后还可以转换回并读取为字符串吗?


What's a good method / easy way to translate a string (lets say 5 characters) into a unique pattern, that afterwards is also convertible back and read as a string?

这是我的代码到现在为止已经开发了,但是在这段代码中,我使用了包含10种不同可能性(->小节大小)的键数组,但是我只喜欢我们4种不同大小。

This is my code I've developed till now, but in this code I used a key-array that includes 10 different possibilities (--> bar sizes) , but I just like to us 4 different sizes.

说明:

我将字符串 hello转换为二进制格式,并将字符串分成最大3得到这样的内容:['001','110','0']。

I' converting my string 'hello' to binary format and splitting the string up into groups of maximum 3 to get something like this: ['001', '110', '0'].

然后,我使用上面的结果数组并在中找到匹配项我的下面的键数组并获取索引(10种不同的索引-> 10种不同的可能性),并将它们用作条形大小。

Afterwards I'm using the result array above and find the matches in my Key-array below and get the indexes (10 different indexed --> 10 different possibilities) and use them as bar-sizes.


但是,必须有一种更有效的方法来翻译字符串变成独特的图案我希望有人可以帮助我改善我的小型算法。预先感谢一百万。

But, There MUST BE a way more efficient method to translate a string into a unique pattern. I hope somebody can help me improve my small algorithm. Thanks a million in advance.

var key = ['0', '1', '000','001','010','100','110','101','011','111']


String.prototype.encode = function() {
  var code = this, result = [],encryped_string=[]
  for (var i=0; i<code.length;i++) result.push(code[i].charCodeAt(0).toString(2).match(/.{1,3}/g));
  for (var i=0; i<result.length; i++) for (var j=0; j<result[i].length; j++) encryped_string.push(key.indexOf(result[i][j]))
  return encryped_string
}



var code = 'hello';
console.log(code.encode())

推荐答案

您似乎正在假设从字符串 Coffee到显示的图形之间存在直接映射。

It appears that you're making the assumption that there is a direct mapping from the string "Coffee" to the graphic that's shown. That assumption is almost certainly incorrect.

首先,考虑一下如果有两首不同的歌曲叫做咖啡会发生什么。您提出的算法将为他们分配相同的代码。那似乎是不合理的。您希望代码唯一地识别歌曲。

First, consider what would happen if there are two different songs called "Coffee." Your proposed algorithm would assign them both the same code. That seems unreasonable. You want the code to uniquely identify the song.

第二,歌曲名称可以任意长。例如,平克·弗洛伊德(Pink Floyd)唱了一首歌,叫做《小毛茸茸动物的几种物种聚集在山洞里,并用Pict开槽。您的编码算法可能无法将其调整为24个小节。即使可以,我总能找到更长的歌曲标题。

Second, song names can be arbitrarily long. For example, there's a song by Pink Floyd called "Several Species of Small Furry Animals Gathered Together in a Cave and Grooving with a Pict." Your encoding algorithm probably won't be able to fit that into 24 bars. Even if it can, I can always find a longer song title.

鉴于字母a-z,可能有11,881,376个5个字符的字符串。如果只想对所有可能值进行唯一编码,则只需23位即可完成。只需将字符串视为以26为基数的数字并进行转换即可。

Given the letters a-z, there are 11,881,376 possible 5-character strings. If you just want to uniquely encode all possible, you can do that with just 23 bits. Just treat the string as a base-26 number and do the conversion.

Spotify最有可能为每首歌曲分配一个唯一的数字,然后对该数字进行编码。字符串 Coffee与您在屏幕上看到的图形代码之间没有直接映射。

Most likely, Spotify is assigning a unique number to each song, and then encoding that number. There is no direct mapping between the string "Coffee" and the graphical code you see on your screen.

这篇关于字符串加密-生成独特的模式,例如Spotify代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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