hash - 如何在 F# 中为 ShipHash 的键创建一个 16 字节的数组?
问题描述
我正在处理需要散列值的代码。SipHash 似乎是一个不错的选择。
let getSipHashValue (buffer:byte []) (key:byte []) =
match key.GetLength(0) with
| 16 -> SipHash24.Hash64(buffer, key)
| _ -> uint64(0)
有没有办法将密钥填充到 16 个字节并确保它有效?
我可以获得确切长度的单词作为键,但我希望能够使用任何单词(小于 16 个字节)并且只使用一些填充。
open System
open System.Text
let testKey : byte [] =
Encoding.UTF8.GetBytes "accumulativeness"
Console.WriteLine("Length: {0}", testKey.GetLength(0))
有没有办法在 F# 中做到这一点?
解决方案
我想我明白了:
open System
open System.Text
let rec getPaddedBytes (s:string) =
let b = Encoding.UTF8.GetBytes s
match b.GetLength(0) with
| 16 -> b
| x when x < 16 -> getPaddedBytes (s + "0")
| _ -> b[0..15]
Console.WriteLine("Length: {0}", testKey.GetLength(0))
let testBytes = getPaddedBytes "accum"
let testString = Encoding.UTF8.GetString testBytes
Console.WriteLine("X: {0}", testString)
我需要修复获取前 16 个字节的问题。不确定该语法。
推荐阅读
- r - 只保留第一次出现为真的最快方法;设置休息为假
- python - 在一维序列的大列表上的 Scipy 二次一维插值非常慢
- python - 如何使用 python-pptx 包自动从 Microsoft Forms 中提取信息?
- r - 在 r 中使用 left_join() 函数后值变为 NA
- fortran - 如何使用两组循环在 Fortran 中打开、读取和写入多个文件?
- javascript - 无法为范围注册 ServiceWorker ('https://.xxx.net/firebase-cloud-messaging-push-scope')
- sql-server - SQL Server - 如何在可能会回滚的运行过程中创建/提交日志记录
- python - 使用从 RoR 到 Flask 的 jwt 时出现错误“无效的加密填充”
- sql - SQL Server - 分组和求和但忽略空值或空值
- for-loop - 带有 json 子列表的 Flutter for 循环