-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(toolkit/crypto): provide
digest
function to compute the hash o…
…f data
- Loading branch information
1 parent
0d1b00b
commit 67eff30
Showing
8 changed files
with
120 additions
and
0 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
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,37 @@ | ||
<a href="/README.md"><img src="/resources/branding/scion-toolkit-banner.svg" height="50" alt="SCION Toolkit"></a> | ||
|
||
| SCION Toolkit | [Projects Overview][menu-projects-overview] | [Changelog][menu-changelog] | [Contributing][menu-contributing] | [Sponsoring][menu-sponsoring] | | ||
| --- | --- | --- | --- | --- | | ||
|
||
## [SCION Toolkit][menu-home] > Crypto | ||
|
||
The NPM sub-module `@scion/toolkit/crypto` provides cryptographic functions. | ||
|
||
To use the crypto module, install the NPM module `@scion/toolkit` as following: | ||
|
||
``` | ||
npm install @scion/toolkit --save | ||
``` | ||
|
||
<details> | ||
<summary><strong>Crypto.digest</strong></summary> | ||
|
||
Generates a digest of the given data using the specified algorithm (or SHA-256 by default) and converts it to a hex string. | ||
|
||
```typescript | ||
import { Crypto } from '@scion/toolkit/crypto'; | ||
|
||
const hash: string = await Crypto.digest('some-data'); | ||
``` | ||
</details> | ||
|
||
|
||
[menu-home]: /README.md | ||
|
||
[menu-projects-overview]: /docs/site/projects-overview.md | ||
|
||
[menu-changelog]: /docs/site/changelog.md | ||
|
||
[menu-contributing]: /CONTRIBUTING.md | ||
|
||
[menu-sponsoring]: /docs/site/sponsoring.md |
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 @@ | ||
{} |
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,19 @@ | ||
/* | ||
* Copyright (c) 2018-2022 Swiss Federal Railways | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
|
||
import {Crypto} from './crypto.util'; | ||
|
||
describe('Crypto', () => { | ||
|
||
it('should hash data', async () => { | ||
const hash = await Crypto.digest('A'); | ||
expect(hash).toEqual('559aead08264d5795d3909718cdd05abd49572e84fe55590eef31a88a08fdffd'); | ||
}); | ||
}); |
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,35 @@ | ||
/* | ||
* Copyright (c) 2018-2022 Swiss Federal Railways | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
|
||
/** | ||
* Provides cryptographic functions. | ||
*/ | ||
export namespace Crypto { | ||
|
||
/** | ||
* Generates a digest of the given data using the specified algorithm (or SHA-256 by default) and converts it to a hex string. | ||
* | ||
* Can only be used in secure contexts (HTTPS) or on localhost. For more information, refer to https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts. | ||
* | ||
* Credits: https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest | ||
*/ | ||
export async function digest(data: string | BufferSource, options?: {algorithm?: 'SHA-256' | 'SHA-384' | 'SHA-512'}): Promise<string> { | ||
if (!isSecureContext) { | ||
throw Error('Function "digest" of @scion/toolkit/crypto is available only in secure contexts (HTTPS) or on localhost. For more information, refer to https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts.'); | ||
} | ||
data = typeof data === 'string' ? new TextEncoder().encode(data) : data; | ||
// Hash the data. | ||
const hashBuffer = await crypto.subtle.digest(options?.algorithm ?? 'SHA-256', data); | ||
// Convert buffer to byte array. | ||
const hashArray = Array.from(new Uint8Array(hashBuffer)); | ||
// Convert bytes to hex string. | ||
return hashArray.map(b => b.toString(16).padStart(2, '0')).join(''); | ||
} | ||
} |
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,17 @@ | ||
/* | ||
* Copyright (c) 2018-2022 Swiss Federal Railways | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
|
||
/* | ||
* Secondary entrypoint: '@scion/toolkit/crypto' | ||
* This module does not depend on Angular. | ||
* | ||
* @see https://github.com/ng-packagr/ng-packagr/blob/master/docs/secondary-entrypoints.md | ||
*/ | ||
export {Crypto} from './crypto.util'; |
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