krot

N2O term encode

Nov 15th, 2020
489
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 0.94 KB | None | 0 0
  1. https://github.com/synrc/n2o/blob/master/src/services/n2o_secret.erl
  2. pickle(Data) ->
  3.     Message = term_to_binary({Data,now()}),
  4.     Padding = size(Message) rem 16,
  5.     Bits = (16-Padding)*8, Key = secret(), IV = crypto:rand_bytes(16),
  6.     Cipher = crypto:block_encrypt(aes_cbc128,Key,IV,<<Message/binary,0:Bits>>),
  7.     Signature = crypto:hmac(sha256,Key,<<Cipher/binary,IV/binary>>),
  8.     base64:encode(<<IV/binary,Signature/binary,Cipher/binary>>).
  9.  
  10. secret() -> wf:config(n2o,secret,<<"ThisIsClassified">>).
  11.  
  12. depickle(PickledData) ->
  13.     try Key = secret(),
  14.         Decoded = base64:decode(wf:to_binary(PickledData)),
  15.         <<IV:16/binary,Signature:32/binary,Cipher/binary>> = Decoded,
  16.         Signature = crypto:hmac(sha256,Key,<<Cipher/binary,IV/binary>>),
  17.         {Data,_Time} = binary_to_term(crypto:block_decrypt(aes_cbc128,Key,IV,Cipher),[safe]),
  18.         Data
  19.     catch E:R -> wf:info(?MODULE,"Depicke Error: ~p",[{E,R}]), undefined end.
Add Comment
Please, Sign In to add comment