mirror of
https://github.com/PrivateBin/PrivateBin.git
synced 2024-03-22 13:10:41 +08:00
remove duplicate code
This commit is contained in:
parent
9d2d7bde42
commit
d0c8975b89
|
@ -851,10 +851,12 @@ jQuery.PrivateBin = (function($, RawDeflate) {
|
||||||
* @param {string} key
|
* @param {string} key
|
||||||
* @param {string} password
|
* @param {string} password
|
||||||
* @param {array} spec cryptographic specification
|
* @param {array} spec cryptographic specification
|
||||||
|
* @param {bool} exportKey
|
||||||
* @return {CryptoKey} derived key
|
* @return {CryptoKey} derived key
|
||||||
*/
|
*/
|
||||||
async function deriveKey(key, password, spec)
|
async function deriveKey(key, password, spec, exportKey)
|
||||||
{
|
{
|
||||||
|
exportKey = exportKey || false;
|
||||||
let keyArray = stringToArraybuffer(key);
|
let keyArray = stringToArraybuffer(key);
|
||||||
if (password.length > 0) {
|
if (password.length > 0) {
|
||||||
// version 1 pastes did append the passwords SHA-256 hash in hex
|
// version 1 pastes did append the passwords SHA-256 hash in hex
|
||||||
|
@ -899,7 +901,7 @@ jQuery.PrivateBin = (function($, RawDeflate) {
|
||||||
name: 'AES-' + spec[6].toUpperCase(), // can be any supported AES algorithm ("AES-CTR", "AES-CBC", "AES-CMAC", "AES-GCM", "AES-CFB", "AES-KW", "ECDH", "DH" or "HMAC")
|
name: 'AES-' + spec[6].toUpperCase(), // can be any supported AES algorithm ("AES-CTR", "AES-CBC", "AES-CMAC", "AES-GCM", "AES-CFB", "AES-KW", "ECDH", "DH" or "HMAC")
|
||||||
length: spec[3] // can be 128, 192 or 256
|
length: spec[3] // can be 128, 192 or 256
|
||||||
},
|
},
|
||||||
false, // the key may not be exported
|
exportKey, // may the key get exported, false by default
|
||||||
['encrypt', 'decrypt'] // we may only use it for en- and decryption
|
['encrypt', 'decrypt'] // we may only use it for en- and decryption
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -935,40 +937,18 @@ jQuery.PrivateBin = (function($, RawDeflate) {
|
||||||
*/
|
*/
|
||||||
me.getCredentials = async function(key, password)
|
me.getCredentials = async function(key, password)
|
||||||
{
|
{
|
||||||
let keyArray = stringToArraybuffer(key);
|
const spec = [
|
||||||
if (password.length > 0) {
|
null, // initialization vector
|
||||||
let passwordArray = stringToArraybuffer(password),
|
key.slice(0, 16), // salt
|
||||||
newKeyArray = new Uint8Array(keyArray.length + passwordArray.length);
|
100000, // iterations
|
||||||
newKeyArray.set(keyArray, 0);
|
256, // key size
|
||||||
newKeyArray.set(passwordArray, keyArray.length);
|
null, // tag size
|
||||||
keyArray = newKeyArray;
|
null, // algorithm
|
||||||
}
|
'gcm', // algorithm mode
|
||||||
|
'none' // compression
|
||||||
// import raw key
|
];
|
||||||
const importedKey = await window.crypto.subtle.importKey(
|
key = key.slice(16);
|
||||||
'raw', // only 'raw' is allowed
|
let derivedKey = await deriveKey(key, password, spec, true);
|
||||||
keyArray.slice(16),
|
|
||||||
{name: 'PBKDF2'}, // we use PBKDF2 for key derivation
|
|
||||||
false, // the key may not be exported
|
|
||||||
['deriveKey'] // we may only use it for key derivation
|
|
||||||
);
|
|
||||||
|
|
||||||
// derive a stronger key for use with AES
|
|
||||||
const derivedKey = await window.crypto.subtle.deriveKey(
|
|
||||||
{
|
|
||||||
name: 'PBKDF2', // we use PBKDF2 for key derivation
|
|
||||||
salt: keyArray.slice(0, 16), // salt used in HMAC
|
|
||||||
iterations: 100000, // amount of iterations to apply
|
|
||||||
hash: {name: 'SHA-256'} // can be "SHA-1", "SHA-256", "SHA-384" or "SHA-512"
|
|
||||||
},
|
|
||||||
importedKey,
|
|
||||||
{
|
|
||||||
name: 'AES-GCM', // can be any supported AES algorithm ("AES-CTR", "AES-CBC", "AES-CMAC", "AES-GCM", "AES-CFB", "AES-KW", "ECDH", "DH" or "HMAC")
|
|
||||||
length: 256 // can be 128, 192 or 256
|
|
||||||
},
|
|
||||||
true, // the key can be exported
|
|
||||||
['encrypt'] // we want to export it
|
|
||||||
);
|
|
||||||
return btoa(
|
return btoa(
|
||||||
arraybufferToString(
|
arraybufferToString(
|
||||||
await window.crypto.subtle.exportKey('raw', derivedKey)
|
await window.crypto.subtle.exportKey('raw', derivedKey)
|
||||||
|
|
|
@ -71,7 +71,7 @@ if ($MARKDOWN):
|
||||||
endif;
|
endif;
|
||||||
?>
|
?>
|
||||||
<script type="text/javascript" data-cfasync="false" src="js/purify-1.0.11.js" integrity="sha512-p7UyJuyBkhMcMgE4mDsgK0Lz70OvetLefua1oXs1OujWv9gOxh4xy8InFux7bZ4/DAZsTmO4rgVwZW9BHKaTaw==" crossorigin="anonymous"></script>
|
<script type="text/javascript" data-cfasync="false" src="js/purify-1.0.11.js" integrity="sha512-p7UyJuyBkhMcMgE4mDsgK0Lz70OvetLefua1oXs1OujWv9gOxh4xy8InFux7bZ4/DAZsTmO4rgVwZW9BHKaTaw==" crossorigin="anonymous"></script>
|
||||||
<script type="text/javascript" data-cfasync="false" src="js/privatebin.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-GNjHU6N7D0xG0WHf1DSrJrGavV+ES+w2t0vgICKD2UJ6g40Y1W+3le0iX7GgC8G6ADBsepMSaEyh47a2adA2HA==" crossorigin="anonymous"></script>
|
<script type="text/javascript" data-cfasync="false" src="js/privatebin.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-JTOr7OLbpnUePy6e/3tj28v3L4Cf1Xvoo1WQVRmJbBAcBK2skOsz4vBkC0RbeRkaObPIYXuv5egrFYseABbuZA==" crossorigin="anonymous"></script>
|
||||||
<!--[if IE]>
|
<!--[if IE]>
|
||||||
<style type="text/css">body {padding-left:60px;padding-right:60px;} #ienotice {display:block;}</style>
|
<style type="text/css">body {padding-left:60px;padding-right:60px;} #ienotice {display:block;}</style>
|
||||||
<![endif]-->
|
<![endif]-->
|
||||||
|
|
|
@ -49,7 +49,7 @@ if ($MARKDOWN):
|
||||||
endif;
|
endif;
|
||||||
?>
|
?>
|
||||||
<script type="text/javascript" data-cfasync="false" src="js/purify-1.0.11.js" integrity="sha512-p7UyJuyBkhMcMgE4mDsgK0Lz70OvetLefua1oXs1OujWv9gOxh4xy8InFux7bZ4/DAZsTmO4rgVwZW9BHKaTaw==" crossorigin="anonymous"></script>
|
<script type="text/javascript" data-cfasync="false" src="js/purify-1.0.11.js" integrity="sha512-p7UyJuyBkhMcMgE4mDsgK0Lz70OvetLefua1oXs1OujWv9gOxh4xy8InFux7bZ4/DAZsTmO4rgVwZW9BHKaTaw==" crossorigin="anonymous"></script>
|
||||||
<script type="text/javascript" data-cfasync="false" src="js/privatebin.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-GNjHU6N7D0xG0WHf1DSrJrGavV+ES+w2t0vgICKD2UJ6g40Y1W+3le0iX7GgC8G6ADBsepMSaEyh47a2adA2HA==" crossorigin="anonymous"></script>
|
<script type="text/javascript" data-cfasync="false" src="js/privatebin.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-JTOr7OLbpnUePy6e/3tj28v3L4Cf1Xvoo1WQVRmJbBAcBK2skOsz4vBkC0RbeRkaObPIYXuv5egrFYseABbuZA==" crossorigin="anonymous"></script>
|
||||||
<!--[if IE]>
|
<!--[if IE]>
|
||||||
<style type="text/css">body {padding-left:60px;padding-right:60px;} #ienotice {display:block;}</style>
|
<style type="text/css">body {padding-left:60px;padding-right:60px;} #ienotice {display:block;}</style>
|
||||||
<![endif]-->
|
<![endif]-->
|
||||||
|
|
Loading…
Reference in New Issue
Block a user