How to generate, encode and encrypt random string in Flutter

Asked

Viewed 1,044 times

1

I need to generate a 32byte code and encode for Base64, then Sha256 and Base64 again

But apparently this code has something wrong and I can’t understand what it is

var _random = Random.secure();
var random = List<int>.generate(32, (i) => _random.nextInt(256));
var verificador = base64Url.encode(random);
var base64ToSha256 = utf8.encode(verificador);
var desafio = base64Url.encode(base64ToSha256);

I tried that:

var uuid = Uuid();
var random = uuid.v4();
var verificador = base64Url.encode(random);
var base64ToSha256 = sha256.convert(verificador);
var desafio = base64Url.encode(base64ToSha256);

but apparently I’m very devoid of intelligence and I’m doing something very wrong...

  • Just one detail: Base64 nay is cryptography, is just one data coding algorithm. Another point is to do sha256(base64(string)) will not bring much more advantages than simply doing sha256(string): any crazy combination of those doesn’t make much difference in terms of security...

  • @hkotsubo thanks for the information but I really need to do so, I am not inventing my own cryptography, I’m just doing things like you asked me to do

  • Actually I put the link because there are other links explaining that it makes no difference to make this combination of Base64 with Base64 hash again. Anyway, it would be interesting to explain to those who asked that it makes no difference this first conversion to Base64 before calculating the hash (then it may make sense depending on how you transmit the data, although for hashes it is more common to represent the bytes as hexadecimal digits, but finally...)

  • @hkotsubo codigo_desafio: [Generate a random 32 bytes, convert to hexadecimal, encode with Base64, replace the "+" and "/" characters with "-" and "_" respectively, and remove the "=" characters to get the code verificador (will be used in the next step), with this SHA256 encryption code and code with Base64 securely to URL (as you did earlier by removing the characters)]

  • 2

    @Aristoeinstein Why do you think you have something wrong with your code? Does it return any errors? Its first code, ran it and ran without returning errors.

  • @Matheusribeiro the first code was wrong sorry This is the error it returns: The argument type 'String' can't be assigned to the parameter type 'List<int>'

Show 1 more comment

1 answer

2


I tested the first code informed by you and it worked correctly...

Following the steps of the exercise cited in a comment by you, I did as follows

import 'dart:convert';
import 'dart:math';

void main() {
  var _random = Random.secure();
  var random = List<int>.generate(32, (i) => _random.nextInt(256));
  var verificador = base64Url.encode(random);
  print(verificador);
  verificador = verificador.replaceAll('+', '-')
    .replaceAll('/', '_')
    .replaceAll('=', '');
  print(verificador);
  var base64ToSha256 = utf8.encode(verificador);
  var desafio = base64Url.encode(base64ToSha256);
  print(desafio);
}

You can test on this site Dartpad;

No error occurred and the expected result was generated.

Make sure you’re not making any wrong steps in your code.

  • 1

    Look at the lack of attention killing me, when I was using the base64Url hoped that he already took the +, =, and the bars, thanks Matheus.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.