hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

equal.ha (754B)


      1 // SPDX-License-Identifier: MPL-2.0
      2 // (c) Hare authors <https://harelang.org>
      3 
      4 // Returns true if the two byte sequences are identical.
      5 //
      6 // This function should NOT be used with sensitive data such as cryptographic
      7 // hashes. In such a case, the constant-time [[crypto::compare]] should be used
      8 // instead.
      9 export fn equal(a: []u8, b: []u8) bool = {
     10 	if (len(a) != len(b)) {
     11 		return false;
     12 	};
     13 	for (let i = 0z; i < len(a); i += 1) {
     14 		if (a[i] != b[i]) {
     15 			return false;
     16 		};
     17 	};
     18 	return true;
     19 };
     20 
     21 @test fn equal() void = {
     22 	let a: []u8 = [1, 2, 3];
     23 	let b: []u8 = [1, 2, 3];
     24 	let c: []u8 = [1, 4, 5];
     25 	let d: []u8 = [1, 2, 3, 4];
     26 	let e: []u8 = [1, 2];
     27 	assert(equal(a, b));
     28 	assert(!equal(a, c));
     29 	assert(!equal(a, d));
     30 	assert(!equal(a, e));
     31 };