candid_text_hash function

int candid_text_hash(
  1. String text
)

The text-hash that is used when using a String for the field name of a Record or Variant, converting the field name to the int representation which is how it is sent over the wire.

Implementation

int candid_text_hash(String text) {
    // hash(id) = ( Sum_(i=0..k) utf8(id)[i] * 223^(k-i) ) mod 2^32 where k = |utf8(id)|-1
    int hash = 0;
    for (int b in utf8.encode(text)) {
        hash = (((hash * 223) % pow(2, 32) as int) + b) % pow(2, 32) as int;
    }
    return hash;
}