Skip to content

Commit ec69ad3

Browse files
hkatzdevrepl.it user
authored and
repl.it user
committed
Initial commit
0 parents  commit ec69ad3

File tree

6 files changed

+142
-0
lines changed

6 files changed

+142
-0
lines changed

.replit

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
run = "deno test -c tsconfig.json test.ts"
2+
language = "deno"

LICENSE

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Copyright (c) 2014-2020, Sideway Inc, and project contributors
2+
Copyright (c) 2015-2020, Vadim Demedes
3+
Copyright (c) 2020, Harrison Katz
4+
All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without
7+
modification, are permitted provided that the following conditions are met:
8+
9+
1. Redistributions of source code must retain the above copyright notice, this
10+
list of conditions and the following disclaimer.
11+
12+
2. Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
16+
3. Neither the name of the copyright holder nor the names of its
17+
contributors may be used to endorse or promote products derived from
18+
this software without specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# secure-compare
2+
3+
Constant-time comparison algorithm to prevent timing attacks for ~~Node.js~~ Deno.
4+
Copied from [cryptiles](https://github.com/hapijs/cryptiles) by [C J Silverio](https://github.com/ceejbot) and from [secure-compare](https://github.com/vadimdemedes/secure-compare) by [Vadim Demedes](https://github.com/vadimdemedes).
5+
6+
### Usage
7+
8+
```typescript
9+
import secureCompare from "https://denopkg.com/hkatzdev/secure-compare/mod.ts";
10+
11+
if (!secureCompare('hello world', 'hello world')) throw Error();
12+
13+
if (!secureCompare('你好世界', '你好世界')) throw Error();
14+
15+
if (secureCompare('hello', 'not hello')) throw Error();
16+
```
17+
18+
[![Run on Repl.it](https://repl.it/badge/github/hkatzdev/secure-compare)](https://repl.it/github/hkatzdev/secure-compare)
19+
20+
### Tests
21+
22+
```
23+
$ deno test
24+
```
25+
26+
27+
### License
28+
29+
secure-compare is released under the BSD 3 Clause license.

mod.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export default (a: string, b: string): boolean => {
2+
let mismatch = a.length === b.length ? 0 : 1;
3+
4+
if (mismatch) b = a;
5+
6+
for (let i = 0, il = a.length; i < il; ++i) {
7+
const ac = a.charCodeAt(i);
8+
const bc = b.charCodeAt(i);
9+
mismatch |= (ac ^ bc);
10+
}
11+
12+
return mismatch === 0;
13+
};

test.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import secureCompare from "./mod.ts";
2+
3+
const a = Math.random().toString(36).substring(2, 15);
4+
const b = Math.random().toString(36).substring(2, 15) +
5+
Math.random().toString(36).substring(2, 15);
6+
7+
Deno.test("Should take the same amount of time comparing different string sizes", () => {
8+
let now = Date.now();
9+
secureCompare(b, a);
10+
const t1 = Date.now() - now;
11+
12+
now = Date.now();
13+
secureCompare(b, b);
14+
const t2 = Date.now() - now;
15+
16+
if (Math.abs(t1 - t2) > 1) {
17+
throw Error(
18+
"Constant time test failed - greater than a 1 millisecond difference.",
19+
);
20+
}
21+
});
22+
23+
Deno.test("Should return true for equal strings", () => {
24+
if (!secureCompare(a, a)) {
25+
throw Error(
26+
"Same string test failed - returned false for identical strings.",
27+
);
28+
}
29+
});
30+
31+
Deno.test("Should return false for different strings (size, a < b)", () => {
32+
if (secureCompare(a, a + "x")) {
33+
throw Error(
34+
"Different string w/ different sizes (a < b) test failed - returned true for different strings.",
35+
);
36+
}
37+
});
38+
39+
Deno.test("Should return false for different strings (size, a > b)", () => {
40+
if (secureCompare(a + "x", a)) {
41+
throw Error(
42+
"Different string w/ different sizes (a > b) test failed - returned true for different strings.",
43+
);
44+
}
45+
});
46+
47+
Deno.test("Should return false for different strings (size, a = b)", () => {
48+
if (secureCompare(a + "x", a + "y")) {
49+
throw Error(
50+
"Different string w/ same size test failed - returned true for different strings.",
51+
);
52+
}
53+
});
54+
55+
Deno.test("Should return true if the strings are identical in utf8", () => {
56+
if (!secureCompare("你好世界", "你好世界")) {
57+
throw Error(
58+
"UTF8 test failed - returned false for identical strings.",
59+
);
60+
}
61+
});

tsconfig.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"compilerOptions": {
3+
"noFallthroughCasesInSwitch": true,
4+
"noUnusedLocals": true,
5+
"noUnusedParameters": true,
6+
"removeComments": true,
7+
}
8+
}

0 commit comments

Comments
 (0)