(F#) 从 CryptoStream 读取所有字节的最平滑方法
我正在研究使用 AesCryptoServiceProvider 解密二进制数据的过程。对于最后一步,检索解密的数据并将其作为字节数组返回,我目前正在使用以下实现:
let rec streamBytes (s : CryptoStream) (b : int) = seq {
if b >= 0 then
yield byte b
yield! streamBytes s (s.ReadByte()) }
streamBytes cryptoStream (cryptoStream.ReadByte())
|> Seq.toArray
它可以工作,但对我来说感觉不“正确”。将 CryptoStream.ReadByte() 的结果作为参数传递给streamBytes(),然后检查该递归调用中的值似乎有点鲁布·戈德堡风格。有更好的方法吗?
I'm working on a procedure for decrypting binary data using AesCryptoServiceProvider. For the final step, retrieving the decrypted data and returning it as an array of bytes, I'm currently using the following implementation:
let rec streamBytes (s : CryptoStream) (b : int) = seq {
if b >= 0 then
yield byte b
yield! streamBytes s (s.ReadByte()) }
streamBytes cryptoStream (cryptoStream.ReadByte())
|> Seq.toArray
It works, but it doesn't feel "correct" to me. Passing the result of CryptoStream.ReadByte() as an argument to streamBytes(), and then checking is value in that recursive call seems a bit Rube Goldberg-y. Is there a better way to be doing it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
逐字节排出流将会非常慢。
如果您有 .NET 4.0 那么最直接的方法是:
否则您需要手动重现 CopyTo 功能
Draining stream byte by byte will be very slow.
if you have .NET 4.0 then the most straightforward way will be:
else you need to reproduce CopyTo functionality manually
是否有理由不使用
Read
而不是ReadBytes
?看起来这样会更直接一些。否则,你的解决方案对我来说看起来还不错 - API 的设计非常有限,因为你需要使用ReadByte
的返回值来确定是否中断循环并决定执行什么操作值到输出。这是一种替代实现:Is there a reason not to use
Read
instead ofReadBytes
? It seems like that would be much more direct. Otherwise, your solution doesn't look too bad to me - the design of the API is pretty limiting, since you need to use the return value ofReadByte
to determine whether to break your loop and to decide what value to output. Here's one alternative implementation: