From C# encryption key derivation to Ruby (PBKDF2) -
i'm trying rewrite following key generation method written in c# ruby equivalent:
private static byte[] createkey(string password, int length) { var salt = new byte[] { 0x01, 0x02, 0x23, 0x34, 0x37, 0x48, 0x24, 0x63, 0x99, 0x04 }; const int iterations = 1000; using (var rfc2898derivebytes = new rfc2898derivebytes(password, salt, iterations)) return rfc2898derivebytes.getbytes(length); }
i'm using pbkdf2 implementation. , here's ruby code:
def create_key password, length salt_a = [0x01, 0x02, 0x23, 0x34, 0x37, 0x48, 0x24, 0x63, 0x99, 0x04] salt = salt_a.pack('c*') # think here there change iterations = 1000 derived_b = pbkdf2.new |p| p.password = password p.salt = salt p.iterations = iterations p.key_length = length p.hash_function = openssl::digest::sha1 end derived_b.bin_string # , here end
in order work 2 methods should return same output. problem can't figure out how this. pbkdf2 implementations takes salt string, c# takes byte array... think problem there.
if can use recent version of openssl, worked me:
salt = [ 0x94, 0x67, 0x16, 0xe6, 0x20, 0xd4, 0x56, 0x46, 0x67, 0x56, 0x46, 0x56, 0x23 ].pack("c*") pbkdf2_iterations = 1000 def create_key(password, length) openssl::pkcs5::pbkdf2_hmac_sha1(password, salt, pbkdf2_iterations, length) end
Comments
Post a Comment