我可以序列化 ruby​​ Digest::SHA1 实例对象吗? [英] Can I serialize a ruby Digest::SHA1 instance object?

查看:30
本文介绍了我可以序列化 ruby​​ Digest::SHA1 实例对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

G'day 的人们,

我正在用 ruby​​ (sinatra) 重新实现现有的自定义文件上传服务,并将 redis 作为后备存储.

I am re-implementing an existent custom file upload service in ruby (sinatra) with redis as a backing store.

客户计算 SHA1 哈希值并开始上传上传最大 64K 块直到完成

Client calculates a SHA1 hash and initiates an upload uploads max 64K chunks until finished

服务器将块附加到文件计算完整文件的 SHA1 哈希值以验证正确接收

Server appends chunks to file calculates SHA1 hash of complete file to verify correct receipt

现在,我希望做的是使用 ruby​​ (1.9.3) Digest::SHA1 <<每个块上的 (update) 操作符,(而不是最后从头开始读取整个文件).[大文件 > 1GB].

Now, what I am hoping to do is use ruby (1.9.3) Digest::SHA1 << (update) operator on each chunk, (rather than having to read the ENTIRE file from scratch at the end). [Large files > 1GB].

不幸的是 Digest::SHA1 和 Marshal.dump 不兼容

Unfortunately Digest::SHA1 and Marshal.dump aren't compatible

1.9.3p125 :001 > require 'digest'
 => true 
1.9.3p125 :002 > $digest = Digest::SHA1.new
 => #<Digest::SHA1: da39a3ee5e6b4b0d3255bfef95601890afd80709> 
1.9.3p125 :003 > marshalled_digest = Marshal.dump($digest)
TypeError: no _dump_data is defined for class Digest::SHA1
    from (irb):3:in `dump'
    from (irb):3
    from /Users/rhodry/.rvm/rubies/ruby-1.9.3-p125/bin/irb:16:in `<main>'
1.9.3p125 :004 > 

有没有人对如何做有任何想法:

Does anyone have any ideas on how to:

  1. 获取对底层内存的访问权限(在 C 中操作)并存储/恢复这样的对象?
  2. 获得允许类似用例的替代实现?

谢谢,

参数

更新:gist:2280705 使用 ruby​​ FFI 实现选项 1 - 希望对其他人有用

Update: gist:2280705 implements option 1 using ruby FFI - hope it is useful to someone else

推荐答案

您是否考虑过并且能够发送 64k 块的 SHA1?会有更多的校验和数据,但你会知道哪里出错了,并且不需要存储摘要的内部状态.

Have you considered and are you able to send the SHA1's of the 64k chunks? There would be more checksum data, but you would know where things went wrong, and there would be no need to store internal state of the digest.

这篇关于我可以序列化 ruby​​ Digest::SHA1 实例对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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