-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
removed crypto-es, use crypto.subtle, clean up
- Loading branch information
Showing
5 changed files
with
171 additions
and
77 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,33 @@ | ||
# Decrypting without Obsidian | ||
|
||
Here are further details in case you ever need to decrypt snippets without Obsidian and this plugin. | ||
|
||
The plugin uses the SubtleCrypto interface of the [Web Crypto API](https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto). | ||
|
||
The result of the AES-GCM encryption is surrounded by markers and Base64 encoded so it can be shown in notes, for example: | ||
|
||
``` | ||
%%🔐 iWPmKEJm7dzCJze3p6TAzVv+F2kYh29kd3FXyOEmHiU= 🔐%% | ||
``` | ||
|
||
After stripping the prefix (%%🔐 ) and suffix ( 🔐%%) from the text, you'll need to convert the base64 encoding back to an array of bytes. Form here, you can decrypt using: | ||
```js | ||
const decryptedBytes = crypto.subtle.decrypt(algorithm, key, bytesToDecrypt) | ||
``` | ||
where: | ||
```js | ||
const algorithm = { | ||
name: 'AES-GCM', | ||
iv: new Uint8Array([196, 190, 240, 190, 188, 78, 41, 132, 15, 220, 84, 211]), | ||
tagLength: 128 | ||
} | ||
//See: https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/encrypt | ||
|
||
const key = await crypto.subtle.importKey( | ||
'raw', | ||
await crypto.subtle.digest({ name: 'SHA-256' }, new TextEncoder().encode(password)), | ||
algorithm, | ||
false, | ||
['encrypt', 'decrypt'] | ||
); | ||
``` |
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 |
---|---|---|
|
@@ -19,8 +19,5 @@ | |
"rollup": "^2.32.1", | ||
"tslib": "^2.0.3", | ||
"typescript": "^4.0.3" | ||
}, | ||
"dependencies": { | ||
"crypto-es": "^1.2.7" | ||
} | ||
} |
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,71 @@ | ||
|
||
const algorithm = { | ||
name: 'AES-GCM', | ||
iv: new Uint8Array([196, 190, 240, 190, 188, 78, 41, 132, 15, 220, 84, 211]), | ||
tagLength: 128 | ||
} | ||
|
||
export default class CryptoHelper { | ||
|
||
private async buildKey(password: string) { | ||
let utf8Encode = new TextEncoder(); | ||
let passwordBytes = utf8Encode.encode(password); | ||
|
||
let passwordDigest = await crypto.subtle.digest({ name: 'SHA-256' }, passwordBytes); | ||
|
||
let key = await crypto.subtle.importKey( | ||
'raw', | ||
passwordDigest, | ||
algorithm, | ||
false, | ||
['encrypt', 'decrypt'] | ||
); | ||
|
||
return key; | ||
} | ||
|
||
public async encryptToBase64(text: string, password: string): Promise<string> { | ||
let key = await this.buildKey(password); | ||
|
||
let utf8Encode = new TextEncoder(); | ||
let bytesToEncrypt = utf8Encode.encode(text); | ||
|
||
// encrypt into bytes | ||
let encryptedBytes = new Uint8Array(await crypto.subtle.encrypt( | ||
algorithm, key, bytesToEncrypt | ||
)); | ||
|
||
//convert array to base64 | ||
let base64Text = btoa(String.fromCharCode(...encryptedBytes)); | ||
|
||
return base64Text; | ||
} | ||
|
||
private stringToArray(str: string): Uint8Array { | ||
var result = []; | ||
for (var i = 0; i < str.length; i++) { | ||
result.push(str.charCodeAt(i)); | ||
} | ||
return new Uint8Array(result); | ||
} | ||
|
||
public async decryptFromBase64(base64Encoded: string, password: string): Promise<string> { | ||
try { | ||
// convert base 64 to array | ||
let bytesToDecrypt = this.stringToArray(atob(base64Encoded)); | ||
|
||
let key = await this.buildKey(password); | ||
|
||
// decrypt into bytes | ||
let decryptedBytes = await crypto.subtle.decrypt(algorithm, key, bytesToDecrypt); | ||
|
||
// convert bytes to text | ||
let utf8Decode = new TextDecoder(); | ||
let decryptedText = utf8Decode.decode(decryptedBytes); | ||
return decryptedText; | ||
} catch (e) { | ||
return null; | ||
} | ||
} | ||
|
||
} |
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