cryptography icon indicating copy to clipboard operation
cryptography copied to clipboard

How to convert a SecretKey to a string

Open mattbreeland opened this issue 4 years ago • 4 comments

toString returns "SecretKey(...)" instead of a String representation of its bytes. But I need a string representation in order to save this to secure storage using https://pub.dev/packages/flutter_secure_storage

Why doesn't toString handle this conversion for me? Can you recommend a workaround?

mattbreeland avatar Sep 22 '20 12:09 mattbreeland

I will go with using "extract" I guess to get a List of ints, then then dart:convert to get the String that I need:

List byteArray = [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]; String secretKeyString = utf8.decode(byteArray);

Ideally this should be baked into the "toString" override though, right?

mattbreeland avatar Sep 22 '20 12:09 mattbreeland

@override
  String toString() => base64Encode(_bytes);


List<int> fromString(String str) => base64Decode(str);

This works, not utf8. Recommend baking it into the SecretKey class!

mattbreeland avatar Sep 22 '20 13:09 mattbreeland

any news?

enzodanjour avatar Aug 18 '21 20:08 enzodanjour

In my case, to convert a string in a key i use:

    String key ="this is my key";//
    var hashAlgor = Sha512();// instance of object
    var hash = await hashAlgor.hash(utf8.encode(key));// convert the key to get a hash of a key

    var secretKey = SecretKeyData(hash.bytes);// get data of key

   var secretBox =
        await algorithm.encrypt(mensageInt, secretKey: secretKey); //pass secret key

enzodanjour avatar Aug 24 '21 20:08 enzodanjour

Thank you for the issue report! I close the issue as I don't think such method will be added.

terrier989 avatar Mar 23 '23 21:03 terrier989