summaryrefslogtreecommitdiff
path: root/std/hash/_fnv/fnv32.ts
blob: 9547e1199f3029a2e6ea87a78b304e9010beb43a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Ported from Go:
// https://github.com/golang/go/tree/go1.13.10/src/hash/fnv/fnv.go
// Copyright 2011 The Go Authors. All rights reserved. BSD license.
// https://github.com/golang/go/blob/master/LICENSE
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.

import { mul32 } from "./util.ts";

const offset32 = 2166136261;
const prime32 = 16777619;

abstract class Fnv32Base<T> {
  #state: number;

  constructor() {
    this.#state = offset32;
  }

  protected _updateState(newState: number): void {
    this.#state = newState;
  }

  reset(): void {
    this.#state = offset32;
  }

  abstract write(data: Uint8Array): T;

  size(): number {
    return 4;
  }

  blockSize(): number {
    return 1;
  }

  sum32(): number {
    return this.#state;
  }

  sum(): Uint8Array {
    return Uint8Array.from([
      (this.#state >> 24) & 0xff,
      (this.#state >> 16) & 0xff,
      (this.#state >> 8) & 0xff,
      this.#state & 0xff,
    ]);
  }
}

/** Fnv32 hash */
export class Fnv32 extends Fnv32Base<Fnv32> {
  write(data: Uint8Array): Fnv32 {
    let hash = this.sum32();

    data.forEach((c) => {
      hash = mul32(hash, prime32);
      hash ^= c;
    });

    this._updateState(hash);
    return this;
  }
}

/** Fnv32a hash */
export class Fnv32a extends Fnv32Base<Fnv32a> {
  write(data: Uint8Array): Fnv32a {
    let hash = this.sum32();

    data.forEach((c) => {
      hash ^= c;
      hash = mul32(hash, prime32);
    });

    this._updateState(hash);
    return this;
  }
}