flutter - How to generate collision free integers from a 24 hex string - Stack Overflow

I am facing an issue where I receive a 24-character hexadecimal string (objectId) from an API, and I ha

I am facing an issue where I receive a 24-character hexadecimal string (objectId) from an API, and I have a function that only accepts ints. How can I generate collision-free integers from this hexadecimal string?

I implemented the following code to convert the hex string into a collision free integer ID:

int generateUniqueIntId(String objectId) {
  String objectIdDigest = objectId.sha256Digest();
  BigInt bigIntHash =
      BigInt.parse(objectIdDigest.toString().substring(0, 16), radix: 16);
  BigInt originalId = BigInt.parse(objectId, radix: 16);
  BigInt combined = bigIntHash ^ originalId;
  return combined.toInt();
}

For example, given this sample objectId:

679f81e1c02ea904a2000000

My Current Problem:

No matter what the objectId is, I always get the following integer output: 9223372036854775807

What I Need:

I need the generated integers to be collision-free for each unique objectId. Specifically, for any given objectId, the generated integer should never be the same as the integer for another objectId.

I am facing an issue where I receive a 24-character hexadecimal string (objectId) from an API, and I have a function that only accepts ints. How can I generate collision-free integers from this hexadecimal string?

I implemented the following code to convert the hex string into a collision free integer ID:

int generateUniqueIntId(String objectId) {
  String objectIdDigest = objectId.sha256Digest();
  BigInt bigIntHash =
      BigInt.parse(objectIdDigest.toString().substring(0, 16), radix: 16);
  BigInt originalId = BigInt.parse(objectId, radix: 16);
  BigInt combined = bigIntHash ^ originalId;
  return combined.toInt();
}

For example, given this sample objectId:

679f81e1c02ea904a2000000

My Current Problem:

No matter what the objectId is, I always get the following integer output: 9223372036854775807

What I Need:

I need the generated integers to be collision-free for each unique objectId. Specifically, for any given objectId, the generated integer should never be the same as the integer for another objectId.

Share Improve this question asked Feb 3 at 0:51 ololoololo 2,0864 gold badges32 silver badges81 bronze badges 4
  • That's mathematically impossible. – Randal Schwartz Commented Feb 3 at 1:00
  • 3 So you want to store 96 bits of data in 64 bits... – beaker Commented Feb 3 at 1:00
  • Unless there are some constraints on the format of the objectId, I think you're going to need a different approach. – beaker Commented Feb 3 at 1:08
  • @beaker there are no constraints on the format of the objectId. It is always a 24 hex string. E.g 679f81e1c02ea904a2000000. I don't mind if the resulting int is signed, I just want a collision free int. – ololo Commented Feb 3 at 1:40
Add a comment  | 

2 Answers 2

Reset to default 2

Since your objectId is a 24 character string of hexadecimal value, it has 12 bytes (or 96 bits) of information.

An int in Dart is 64 bits (or 8 bytes).

So there is no way to map each possible value from your objectId to a guaranteed unique value in the int. There will by definition always be possible collisions.

This is nearly impossible.

However, if you have any sort of control over the objectIds from the API then I can assume we are working within a controlled space from where the objectIds are generated and not any kind of universal objectId.

In that case you can try this out, this just picks the first valid integer that can fit into dart's int max from the objectId.

int generateUniqueIntId(String objectId) {
  BigInt originalId = BigInt.parse(objectId, radix: 16);
  String originalIdStr = originalId.toString();
  BigInt dartIntMax = (BigInt.one << 63) - BigInt.one;
  String maxIntStr = dartIntMax.toString();
  int length = maxIntStr.length;
  BigInt uniqueInt;
  do {
    uniqueInt = BigInt.parse(originalIdStr.substring(0, length));
    length--;
  } while (uniqueInt > dartIntMax);
  return uniqueInt.toInt();
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745253041a4618791.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信