-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING CHANGE: switch from crypto-js to webcrypto api (resolve #42)
- Loading branch information
Showing
7 changed files
with
162 additions
and
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,55 @@ | ||
'use strict'; | ||
|
||
angular.module('anchrClientApp') | ||
.controller('ViewImageCtrl', ['$scope', '$rootScope', '$http', '$location', 'Snackbar', 'Image', 'id', function ($scope, $rootScope, $http, $location, Snackbar, Image, id) { | ||
.controller('ViewImageCtrl', ['$scope', '$rootScope', '$http', '$location', 'Snackbar', 'Image', 'Encryption', 'id', function ($scope, $rootScope, $http, $location, Snackbar, Image, Encryption, id) { | ||
$rootScope.init(); | ||
|
||
$scope.image = {}; | ||
$scope.data = { | ||
password : '', | ||
imgSrc : null, | ||
password: '', | ||
imgSrc: null, | ||
loading: false | ||
}; | ||
|
||
Image.get.get({id: id}, function(result) { | ||
Image.get.get({ id: id }, function (result) { | ||
$scope.image = result; | ||
$scope.image.created = Date.parse($scope.image.created); | ||
$scope.image.link = $location.absUrl(); | ||
}); | ||
|
||
$scope.decrypt = function () { | ||
var relativeHref = new URL($scope.image.href).pathname | ||
$scope.data.loading = true; | ||
$http.get(relativeHref).then(function (result) { | ||
var password = $scope.data.password; | ||
var reader = new FileReader(); | ||
var blob = new Blob([result.data], { type: $scope.image.type }); | ||
|
||
reader.onload = function(e){ | ||
|
||
var decrypted = CryptoJS.AES.decrypt(e.target.result, password).toString(CryptoJS.enc.Latin1); | ||
|
||
if(!/^data:/.test(decrypted)){ | ||
$scope.data.loading = false; | ||
$scope.data.password = ''; | ||
$scope.$apply(); | ||
Snackbar.show("Invalid password."); | ||
return false; | ||
} | ||
|
||
$scope.data.imageSrc = decrypted; | ||
$scope.data.loading = false; | ||
$scope.$apply(); | ||
}; | ||
|
||
reader.readAsText(blob); | ||
|
||
}, function (err) { | ||
console.log(err) | ||
}); | ||
var relativeHref = new URL($scope.image.href).pathname | ||
$http.get(relativeHref, { responseType: 'arraybuffer' }) | ||
.then(function (result) { | ||
var password = $scope.data.password; | ||
|
||
Encryption.decrypt(result.data, password) | ||
.then(function (decrypted) { | ||
var reader = new FileReader(); | ||
var blob = new Blob([decrypted], { type: $scope.image.type }); | ||
|
||
reader.onload = function (event) { | ||
var result = event.target.result; | ||
|
||
$scope.data.imageSrc = result; | ||
$scope.data.loading = false; | ||
$scope.$apply(); | ||
} | ||
|
||
reader.readAsDataURL(blob) | ||
}) | ||
.catch(function (error) { | ||
console.error(error); | ||
|
||
$scope.data.loading = false; | ||
$scope.data.password = ''; | ||
$scope.$apply(); | ||
Snackbar.show("Invalid password."); | ||
}) | ||
}, function (err) { | ||
console.log(err) | ||
}); | ||
}; | ||
}]); |
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,102 @@ | ||
function appendBuffer(buffer1, buffer2) { | ||
var tmp = new Uint8Array(buffer1.byteLength + buffer2.byteLength); | ||
tmp.set(new Uint8Array(buffer1), 0); | ||
tmp.set(new Uint8Array(buffer2), buffer1.byteLength); | ||
return tmp.buffer; | ||
}; | ||
|
||
function getKeyMaterial(password) { | ||
let enc = new TextEncoder(); | ||
return window.crypto.subtle.importKey( | ||
'raw', | ||
enc.encode(password), | ||
{ name: 'PBKDF2' }, | ||
false, | ||
['deriveBits', 'deriveKey'] | ||
); | ||
} | ||
|
||
function getKey(keyMaterial, salt) { | ||
return window.crypto.subtle.deriveKey( | ||
{ | ||
'name': 'PBKDF2', | ||
salt: salt, | ||
'iterations': 100000, | ||
'hash': 'SHA-256' | ||
}, | ||
keyMaterial, | ||
{ 'name': 'AES-GCM', 'length': 256 }, | ||
true, | ||
['encrypt', 'decrypt'] | ||
); | ||
} | ||
|
||
function encrypt(data, key, iv) { | ||
return window.crypto.subtle.encrypt( | ||
{ | ||
name: 'AES-GCM', | ||
iv: iv | ||
}, | ||
key, | ||
data | ||
) | ||
} | ||
|
||
function decrypt(data, key, iv) { | ||
return window.crypto.subtle.decrypt( | ||
{ | ||
name: 'AES-GCM', | ||
iv: iv | ||
}, | ||
key, | ||
data | ||
) | ||
} | ||
|
||
angular.module('anchrClientApp') | ||
.factory('Encryption', function () { | ||
return { | ||
/** | ||
* Encrypts an image with a password | ||
* @param {ArrayBuffer} data The image to be encrypted | ||
* @param {string} password The password to use for encryption | ||
* @returns {ArrayBuffer} The encrypted binary image data, prefixed with 16 bytes of salt, followed by 12 bytes of initialization vector | ||
*/ | ||
encrypt: function (data, password) { | ||
var salt = window.crypto.getRandomValues(new Uint8Array(16)); | ||
var iv = window.crypto.getRandomValues(new Uint8Array(12)); | ||
|
||
return getKeyMaterial(password) | ||
.then(function (keyMaterial) { | ||
return getKey(keyMaterial, salt); | ||
}) | ||
.then(function (key) { | ||
return encrypt(data, key, iv); | ||
}) | ||
.then(function (result) { | ||
var prefix = appendBuffer(salt, iv); | ||
return appendBuffer(prefix, result); | ||
}); | ||
}, | ||
/** | ||
* Decrypts an image using a password | ||
* @param {ArrayBuffer} data The encrypted binary image data, prefixed with 16 bytes of salt, followed by 12 bytes of initialization vector | ||
* @param {string} password The password previously used for encryption | ||
* @returns {ArrayBuffer} The decrypted image content | ||
*/ | ||
|
||
decrypt: function (data, password) { | ||
var salt = data.slice(0, 16); | ||
var iv = data.slice(16, 12 + 16); | ||
var image = data.slice(12 + 16); | ||
|
||
return getKeyMaterial(password) | ||
.then(function (keyMaterial) { | ||
return getKey(keyMaterial, salt); | ||
}) | ||
.then(function (key) { | ||
return decrypt(image, key, iv); | ||
}); | ||
} | ||
} | ||
}); |
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