简体   繁体   English

整数到IV类型的转换

[英]Integer to IV type conversion

I am using AES256 from this library and types from this library. 我使用AES256从这个库和类型从这个库。 here ctrCombine function takes IV (Initialisation Vector) in IV cipher type. 这里的ctrCombine函数采用IV cipher类型的IV (初始化向量)。

Instead of generating random IV from makeIV function, I want to generate it from some Integers that I already have. 与其从makeIV函数生成随机IVmakeIV从我已经拥有的一些Integer中生成它。

  1. Is it possible to convert any Integer to 'IV cipher' type? 是否可以将任何整数转换为“ IV密码”类型?

  2. If it is possible how can I do it? 如果可以的话我该怎么办?

makeIV doesn't generate a random IV as you implied. makeIV不会像您暗示的那样生成随机IV。 It is exactly the function you want. 正是您想要的功能。 Just serialize your integer to a bytestring of sufficient length then call makeIV on that bytestring: 只需将整数序列化为足够长的字节串,然后对该字节串调用makeIV:

makeIV $ runPut (putWord64be (fromIntegral i) >>putWord64be (fromIntegral (i `shiftR` 64)))

where the put operations are from the cereal package. cereal包装中进行放置操作。 You could use binary instead of cereal but then you'd have to make sure you get a strict bytestring. 您可以使用binary代替cereal但是必须确保得到严格的字节串。

EDIT: A more complete example: 编辑:一个更完整的例子:

import Data.Binary.Put
import Crypto.Cipher.Types
import Crypto.Cipher.AES (AES256)
import Data.ByteArray (unpack)
import qualified Data.ByteString.Lazy as LBS
import Data.Bits (shiftR)

example :: Integer -> IV AES256
example i =
  maybe (error "foo") id $
  -- ^^ makeIV returns a `Maybe` (Nothing for IVs of incorrect size
  makeIV $ LBS.toStrict $
  --      ^^ makeIV requires strict bytestrings
     runPut (putWord64be (fromIntegral i) >>
             putWord64be (fromIntegral (i `shiftR` 64)))
  -- ^^ Construct your IV bytestring however you'd like

main = do print $ unpack (example 0)
          print $ unpack (example 1)
          print $ unpack (example (2^63))
          print $ unpack (example (2^65))
          print $ unpack (example (2^112))
          print $ unpack (example (2^120))

Notice that this DOES NOT use crypto-cipher-types since that is sort of parallel to and separate from cryptonite. 请注意,此操作不使用加密密码类型,因为这与加密对象平行且分离。 You should use the IV type from cryptonite instead (see Cryptonite's Crypto.Cipher.Types modules). 您应该改用cryptonite的IV类型(请参阅Cryptonite的Crypto.Cipher.Types模块)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM