-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5490b32
commit 55e3961
Showing
20 changed files
with
141 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--- | ||
title: "addToDate()" | ||
--- | ||
|
||
# `addToDate()` | ||
|
||
Creates a new `Date` by adding the provided time-span to the one provided. Supports negative time spans. | ||
|
||
## Definition | ||
|
||
```ts | ||
//$ TimeSpan=/reference/main/TimeSpan | ||
function createDate(date: Date, timeSpan: $$TimeSpan): Date; | ||
``` | ||
|
||
### Parameters | ||
|
||
- `date` | ||
- `timeSpan` | ||
|
||
## Example | ||
|
||
```ts | ||
import { addToDate, TimeSpan } from "oslo"; | ||
|
||
const tomorrow = addToDate(new Date(), new TimeSpan(1, "d")); | ||
``` |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { expect, test } from "vitest"; | ||
import { constantTimeEqual } from "./bytes.js"; | ||
|
||
test("compareBytes()", () => { | ||
const randomBytes = new Uint8Array(32); | ||
crypto.getRandomValues(randomBytes); | ||
expect(constantTimeEqual(randomBytes, randomBytes)).toBe(true); | ||
const anotherRandomBytes = new Uint8Array(32); | ||
crypto.getRandomValues(anotherRandomBytes); | ||
expect(constantTimeEqual(randomBytes, anotherRandomBytes)).toBe(false); | ||
expect(constantTimeEqual(new Uint8Array(0), new Uint8Array(1))).toBe(false); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export function constantTimeEqual(a: Uint8Array, b: Uint8Array): boolean { | ||
if (a.length !== b.length) { | ||
return false; | ||
} | ||
let c = 0; | ||
for (let i = 0; i < a.length; i++) { | ||
c |= a[i]! ^ b[i]!; | ||
} | ||
return c === 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/crypto/ecdsa.test.ts → src/crypto/signing-algorithm/ecdsa.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/crypto/hmac.test.ts → src/crypto/signing-algorithm/hmac.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export { ECDSA } from "./ecdsa.js"; | ||
export { HMAC } from "./hmac.js"; | ||
export { RSASSAPKCS1v1_5, RSASSAPSS } from "./rsa.js"; | ||
|
||
export type { ECDSACurve } from "./ecdsa.js"; | ||
|
||
export type { SigningAlgorithm, KeyPair } from "./shared.js"; |
2 changes: 1 addition & 1 deletion
2
src/crypto/rsa.test.ts → src/crypto/signing-algorithm/rsa.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export interface SigningAlgorithm { | ||
sign(key: Uint8Array, data: Uint8Array): Promise<Uint8Array>; | ||
verify(key: Uint8Array, signature: Uint8Array, data: Uint8Array): Promise<boolean>; | ||
} | ||
|
||
export interface KeyPair { | ||
publicKey: Uint8Array; | ||
privateKey: Uint8Array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,3 @@ | ||
export type TimeSpanUnit = "ms" | "s" | "m" | "h" | "d" | "w"; | ||
export { TimeSpan, isWithinExpirationDate, addToDate } from "./time.js"; | ||
|
||
export class TimeSpan { | ||
constructor(value: number, unit: TimeSpanUnit) { | ||
this.value = value; | ||
this.unit = unit; | ||
} | ||
|
||
public value: number; | ||
public unit: TimeSpanUnit; | ||
|
||
public milliseconds(): number { | ||
if (this.unit === "ms") { | ||
return this.value; | ||
} | ||
if (this.unit === "s") { | ||
return this.value * 1000; | ||
} | ||
if (this.unit === "m") { | ||
return this.value * 1000 * 60; | ||
} | ||
if (this.unit === "h") { | ||
return this.value * 1000 * 60 * 60; | ||
} | ||
if (this.unit === "d") { | ||
return this.value * 1000 * 60 * 60 * 24; | ||
} | ||
return this.value * 1000 * 60 * 60 * 24 * 7; | ||
} | ||
|
||
public seconds(): number { | ||
return this.milliseconds() / 1000; | ||
} | ||
|
||
public transform(x: number): TimeSpan { | ||
return new TimeSpan(Math.round(this.milliseconds() * x), "ms"); | ||
} | ||
} | ||
|
||
export function isWithinExpirationDate(date: Date): boolean { | ||
return Date.now() < date.getTime(); | ||
} | ||
|
||
export function createDate(timeSpan: TimeSpan): Date { | ||
return new Date(Date.now() + timeSpan.milliseconds()); | ||
} | ||
export type { TimeSpanUnit } from "./time.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
export type TimeSpanUnit = "ms" | "s" | "m" | "h" | "d" | "w"; | ||
|
||
export class TimeSpan { | ||
constructor(value: number, unit: TimeSpanUnit) { | ||
this.value = value; | ||
this.unit = unit; | ||
} | ||
|
||
public value: number; | ||
public unit: TimeSpanUnit; | ||
|
||
public milliseconds(): number { | ||
if (this.unit === "ms") { | ||
return this.value; | ||
} | ||
if (this.unit === "s") { | ||
return this.value * 1000; | ||
} | ||
if (this.unit === "m") { | ||
return this.value * 1000 * 60; | ||
} | ||
if (this.unit === "h") { | ||
return this.value * 1000 * 60 * 60; | ||
} | ||
if (this.unit === "d") { | ||
return this.value * 1000 * 60 * 60 * 24; | ||
} | ||
return this.value * 1000 * 60 * 60 * 24 * 7; | ||
} | ||
|
||
public seconds(): number { | ||
return this.milliseconds() / 1000; | ||
} | ||
|
||
public transform(x: number): TimeSpan { | ||
return new TimeSpan(Math.round(this.milliseconds() * x), "ms"); | ||
} | ||
} | ||
|
||
export function isWithinExpirationDate(date: Date): boolean { | ||
return Date.now() < date.getTime(); | ||
} | ||
|
||
export function addToDate(date: Date, timeSpan: TimeSpan): Date { | ||
return new Date(date.getTime() + timeSpan.milliseconds()); | ||
} |