Using base64-bytestring with lazy ByteStrings

Here is what I am trying to do in Haskell:

  • accept a message in ByteString format (it doesn't really matter if it's lazy or strict)
  • encrypt RSA public key message
  • base64 encodes an encrypted message

the RSA library that I use to handle lazy ByteStrings inside. However, the Base64 library uses only strict byte strings. My application uses lazy ByteStrings to send messages to network sockets.

So it looks like I need to convert between lazy and strict ByteStrings. That's what I'm doing:

encrypt :: CryptoRandomGen t => t -> RSA.PublicKey -> L.ByteString -> L.ByteString
encrypt gen pubkey msg = do
  let (ciphertext,_) = RSA.encrypt gen pubkey msg
  (L.fromChunks . map encode . L.toChunks) $ ciphertext

decrypt :: RSA.PrivateKey -> L.ByteString -> Either String L.ByteString
decrypt privkey ciphertext = do
  dec <- decode $ S.concat $ L.toChunks ciphertext
  return $ RSA.decrypt privkey $ L.fromChunks [dec]

, . , , , . , : ByteStrings base64? ?

Lazy ByteStrings - ByteString. , ?

, .

+3
1

, base64- (3 8 ) (4 6 ) , , , . , , .

> encode "Haskell"
"SGFza2VsbA=="
> encode "Hask" `append` encode "ell"
"SGFzaw==ZWxs"

, , =, . .

, , , ( ) , .

, , , ( ).

+4

All Articles