【问题标题】:The dart implementation behind String.hashCode for use in other language用于其他语言的 String.hashCode 背后的 dart 实现
【发布时间】:2020-01-06 18:53:08
【问题描述】:

我在 Flutter 应用程序中错误地依赖 String.hashCode 的值并将其存储在服务器端。现在我希望能够在 nodeJS 中计算一个字符串的哈希码并得到与 dart 相同的结果...

我怎样才能实现它?在dart中没有找到String.hashCode的实际实现。

【问题讨论】:

    标签: flutter dart hashcode


    【解决方案1】:

    String.hashCode的vm实现可以在github上的sdk repo中找到(https://github.com/dart-lang/sdk):

    例子:

    【讨论】:

      【解决方案2】:

      这是一个简单的 Swift 实现,它应该为 iOS 和 Flutter 中的简单字符串返回相同的 hashCode 值:

      extension String {
        func hashCode() -> UInt32 {
          var hash: UInt32 = 0
          for i in unicodeScalars.filter({ $0.isASCII }).map({ $0.value }) {
            hash = hash &+ i
            hash = hash &+ (hash << 10)
            hash = hash ^ (hash >> 6)
          }
          hash = hash &+ (hash << 3)
          hash = hash ^ (hash >> 11)
          hash = hash &+ (hash << 15)
          hash = hash & ((1 << 30) &- 1)
          return (hash == 0) ? 1 : hash
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-18
        • 2010-10-13
        • 1970-01-01
        • 2011-02-27
        • 2016-03-20
        • 1970-01-01
        相关资源
        最近更新 更多