-
Notifications
You must be signed in to change notification settings - Fork 292
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' of https://github.com/wireapp/wire-webapp into imp…
…rovements/certificate-renewal
- Loading branch information
Showing
9 changed files
with
318 additions
and
193 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
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,72 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2024 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
* | ||
*/ | ||
|
||
import {Encoder, Decoder} from 'bazinga64'; | ||
|
||
export class EncryptedStorage { | ||
private encryptionKey: Promise<CryptoKey>; | ||
length = Promise.resolve(0); | ||
|
||
constructor(secretKey: Uint8Array) { | ||
this.encryptionKey = crypto.subtle.importKey('raw', secretKey, 'AES-GCM', false, ['encrypt', 'decrypt']); | ||
} | ||
|
||
async setItem(key: string, value: string) { | ||
try { | ||
const encryptionKey = await this.encryptionKey; | ||
const iv = window.crypto.getRandomValues(new Uint8Array(12)); | ||
const encryptedValue = await window.crypto.subtle.encrypt( | ||
{name: 'AES-GCM', iv}, | ||
encryptionKey, | ||
Encoder.toBase64(value).asBytes, | ||
); | ||
const base64Value = Encoder.toBase64(encryptedValue).asString; | ||
window.localStorage.setItem(key, JSON.stringify({value: base64Value, iv: Array.from(iv)})); | ||
} catch (error) { | ||
console.error(error); | ||
throw error; | ||
} | ||
} | ||
|
||
async getItem(key: string) { | ||
const entry = localStorage.getItem(key); | ||
if (entry) { | ||
const encryptionKey = await this.encryptionKey; | ||
const {value, iv} = JSON.parse(entry); | ||
const decrypted = await crypto.subtle.decrypt( | ||
{name: 'AES-GCM', iv: new Uint8Array(iv)}, | ||
encryptionKey, | ||
Decoder.fromBase64(value).asBytes, | ||
); | ||
return Decoder.fromBase64(Array.from(new Uint8Array(decrypted))).asString; | ||
} | ||
return null; | ||
} | ||
|
||
async removeItem(key: string) { | ||
localStorage.removeItem(key); | ||
} | ||
|
||
async clear() { | ||
localStorage.clear(); | ||
} | ||
async key(): Promise<null> { | ||
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
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
Oops, something went wrong.