1

Is there a Library in Dart to generate one string or hash from 2 different String despite the order of their occurrence. Example:

String first = "xyz";
String second = "abcd"; 

String generated1 = genrateString(first,second);
String generated2 = genrateString(second, first);
AssertTrue(generated1 == generated2)

generated1 And generated2 should be an identical string string

2 Answers 2

3

You could try sorting them into alphabetical order then using the hashcode. (Note that this hash is an int).

int generateHash(String s1, String s2) =>
    (<String>[s1, s2]..sort()).join().hashCode;
Sign up to request clarification or add additional context in comments.

Comments

0

I think we can use String.compareTo and MD5 to make simple one (hash collision will still exist).

Steps:

  1. uses String.compareTo to check two input strings

  2. if compreTo returns >0, then uses MD5 to get the hash value for "$s1${s1.length}$s2${s2.length}", otherwise, uses "$s2${s2.length}$s1${s1.length}". Please see first two test cases in the snippet to see why adding String.length into the calculation.

Snippet:

import 'dart:convert';
import 'package:crypto/crypto.dart';

String genHash(String s1, String s2) {
  if (s1.compareTo(s2) > 0) return md5.convert(utf8.encode("$s1${s1.length}$s2${s2.length}")).toString();
  else return md5.convert(utf8.encode("$s2${s2.length}$s1${s1.length}")).toString();
}
void main() {
  String s1 = '11';
  String s2 = '12';
  print(genHash(s1, s2));
  print(genHash(s2, s1));
  assert(genHash(s1, s2) == genHash(s2, s1));
  String s3 = '1';
  String s4 = '121';
  print(genHash(s3, s4));
  print(genHash(s4, s3));
  assert(genHash(s3, s4) == genHash(s4, s3) && genHash(s4, s3) != genHash(s2, s1));
  s1 = '1234';
  s2 = '123';
  print(genHash(s1, s2));
  print(genHash(s2, s1));
  assert(genHash(s1, s2) == genHash(s2, s1));
  s1 = '123';
  s2 = '123';
  print(genHash(s1, s2));
  print(genHash(s2, s1));
  assert(genHash(s1, s2) == genHash(s2, s1));
  s1 = '1';
  s2 = '2';
  print(genHash(s1, s2));
  print(genHash(s2, s1));
  assert(genHash(s1, s2) == genHash(s2, s1));
  s1 = '';
  s2 = '';
  print(genHash(s1, s2));
  print(genHash(s2, s1));
  assert(genHash(s1, s2) == genHash(s2, s1));
  s1 = ' ';
  s2 = '';
  print(genHash(s1, s2));
  print(genHash(s2, s1));
  assert(genHash(s1, s2) == genHash(s2, s1));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.