如何在Ruby中加密图像字节数组

时间:2018-09-04 13:18:22

标签: java ruby sha1 hmac

Java示例代码中的要求:

MultipartFile file; // image file
Long timestamp = System.currentTimeMillis();
byte[] fileBytes = file.getBytes();
byte[] timeBytes = String.valueOf(timestamp).getBytes();
byte[] digestBytes = ArrayUtils.addAll(fileBytes, timeBytes);
String result = hmac_sha1(digestBytes, key);

将其翻译为Ruby:

f = open(file)
file = File.binread(f)

# Here I tried different ways to get the data string
data = f + timestamp # way 1
data = f.unpack("B*") + timestamp.unpack("B*") # way 2

digest = OpenSSL::Digest.new('sha1')
result = OpenSSL::HMAC.hexdigest(digest, key, data)

所以我的问题是如何计算“数据”,以使我的Ruby版本结果等于我的Java版本结果。

0 个答案:

没有答案