对字符串进行哈希处理我想要 JMP 中的短字符串哈希函数。 (我需要它从行数据创建匿名整数数据库键。)
它不需要加密质量,只需要具有 64 位(或 128 位)结果并且具有相当低的冲突率。 我尝试使用 Blob MD5() (截断为 64 位),但我不敢相信它有多慢。 (只需在 500,000 行的表上尝试一下即可!)。有人在 JSL 中创建或链接到快速哈希算法吗? 使用输入字符串的某些函数来播种 JSL 的随机数生成器怎么样? 我不需要 NIST 质量,但我确实需要一些快速且不太脏的东西,以便在所有平台上给出相同的结果。
您可以从 JSL 中调用现有的众多 C++ 或 Java 哈希算法之一吗? 如何?
PS 也许 JMP 14.1 可以在其标准库中包含一些更好的哈希函数。 包括 SHA 怎么样?
回复:哈希字符串顺便说一句,这是我最终确定的解决方案:
// ==========================
// ::String Hash()
// ==========================
myStringHash = New Custom Function(
"Global",
"String Hash",
Function( {str, length = 64},
{bytes},
bytes = Match( length, 64, 8, 32, 4, Throw( "Bad argument in ::String Hash; hash length must be 64 (default) or 32." ) );
Subscript( Blob To Matrix( Blob Peek( Blob MD5( Char To Blob( str, "utf-8" ) ), 0, bytes ), "int", bytes, "little" ), 1 );
)
);
myStringHash << Description(
"Accepts a string as input, and returns an unsigned 64-bit or 32-bit integer hash of the string, generated by truncation of the (128-bit) MD5 hash of the input string.
The first parameter is the input string to be hashed.
The second, optional parameter is either the number 64 or the number 32. This selects whether the result will be a 64-bit (8-byte) integer, or a 32-bit (4-byte) integer. If not specified, the function defaults to 64-bit (8-byte). If a value other than 64 or 32 is entered, an error results."
);
myStringHash << Prototype( "::String Hash (string, <64|32>)" );
myStringHash << Example( "z = ::String Hash ( \!"This is a string\!" ); Show ( Format( z, 20, 0) )" );
myStringHash << Parameter( "String", "Input string" );
myStringHash << Parameter( "Number", "<64 or 32 (default is 64)>" );
myStringHash << Formula Category( "Database" );
Add Custom Functions( {myStringHash} );回复:哈希字符串克雷格,
也许我对 Blob MD5 太严格了。 它比我的机器上使用输入字符串的速度慢。 尽管如此,还是感谢您的客观检查。
约翰
回复:哈希字符串这在 1,000,000 行上运行大约需要 10 秒。
start = Tick Seconds();
dt = New Table( "Untitled",
Add Rows( 1e6 ),
New Column( "Column 1", Numeric, "Continuous", Format( "Best", 12 ), Formula( Row() ) ),
New Column( "Column 2", Character, "Nominal", Formula( Hex( Blob MD5( Char To Blob( Char( :Column 1 ) ) ) ) ) )
);
dt << runformulas; // run the formulas now, not in the background
stop = Tick Seconds();
Show( stop - start );
Using MD5 for hashUsing MD5 for hash使用 MD5 进行哈希
回复:哈希字符串感谢这个解决方案!
不幸的是,我使用的是Mac,所以我实际上无法使用vbs。 但现在我从你的例子中看到了如何使用 RunProgram,我将尝试让它为 CityHash 工作( https://opensource.googleblog.com/2011/04/introducing-cityhash.html )。
回复:哈希字符串约翰·马登,
我不是专家,这可能无法满足您的需求。 下面是用于 SHA256 和 MD5 加密的 VBscript,可通过此链接找到它。
https://gist.github.com/jermity/557de47a978f7a7c4a74
我只知道Windows。复制附件,并将扩展名更改为 .vbs。 打开命令窗口,cd 到保存目录并输入 jerhash.vbs 以获取使用语法。嵌入的脚本转换状态名称。 此 vbs 脚本似乎不允许使用值列表。 52 个状态名称花费了 8.2 秒,我确信连接运行程序的开销会增加这一点。 附加的脚本显示了创建 .bat 文件的替代方法。 52 个项目的平均耗时约为 0.1 秒。 我没有读到该文件。 这是另一种方法。 离线生成 500k 然后读入文件。
这就是我的全部。问候。

mytbl = New Table("testit", New Column("States", Character,
Values({"Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut",
"District of Columbia", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho",
"Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine",
"Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri",
"Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico",
"New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon",
"Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee",
"Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin",
"Wyoming"}) ), NewColumn("Code",Character)
);
t1 = tick seconds();
for(i=1, i<=nrow(mytbl), i++,
:Code[i] = Trim(RunProgram(Executable("cscript"),
Options(EvalInsert("\[//nologo c:\temp\jerhash.vbs /A:sha256 /S:"^:States[i]^"]\") ),
ReadFunction("text")
));
);
t2= tickseconds();
show(t2-t1);