Skip to content

Commit

Permalink
Encryption Enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidTimms committed Jun 21, 2013
1 parent 5327fb5 commit 0d92b71
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
18 changes: 15 additions & 3 deletions static/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ function Chat($scope) {
if (!$scope.server) {
window.ng_scope = $scope;
$scope.chat_url = $scope.chat_url || location.pathname.substring(1);
$scope.key = location.hash.substring(1);
console.log($scope.key);
}
var i;
$scope.chat_name = $scope.chat_name || $scope.chat_url.split('_').join(' ') || 'Chat';
$scope.my_username = undefined;
$scope.locked = false;

$scope.chatters = [];
$scope.chatters.get = function (name) {
for (var i = 0; i<this.length; i++) {
Expand Down Expand Up @@ -140,6 +141,7 @@ function Chat($scope) {
$scope.chatters.pop();
}
for (i = 0; i < data.messages.length; i++) {
data.messages[i].text = $scope.decrypt(data.messages[i].text);
new $scope.Message(data.messages[i]);
}
for (i = 0; i < data.chatters.length; i++) {
Expand All @@ -151,17 +153,20 @@ function Chat($scope) {
});
socket.on('new message', function (data) {
if (data.sender !== $scope.my_username || !data.sender) {
console.log('message: ' + data.text);
data.text = $scope.decrypt(data.text);
new $scope.Message(data);
}
$scope.$apply();
// Scroll to the bottom jsut for prettyness
});
$scope.sendMessage = function() {
if ($scope.message_text !== '') {
var message = new $scope.Message({text: $scope.message_text, sender: $scope.my_username});
var original_text = $scope.message_text;
var enc_text = 'ENCRYPTED: ' + GibberishAES.enc(original_text, $scope.key);
var message = new $scope.Message({text: enc_text, sender: $scope.my_username});
$scope.message_text = '';
socket.emit('new message', message);
message.text = original_text;
}
};
// Send message on enter key
Expand Down Expand Up @@ -289,6 +294,13 @@ function Chat($scope) {
$scope.$apply();
});

$scope.decrypt = function (text) {
if (text.indexOf('ENCRYPTED:') === 0) {
text = GibberishAES.dec(text.substring(11), $scope.key);
}
return text;
}

// Sidebar sliding
var slide_speed = 300;
var sidebar = $('.left-column');
Expand Down
12 changes: 11 additions & 1 deletion static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@
}
</style>
<script>
function generateKey () {
var key = '';
for (var i = 0; i < 64; i += 1) {
var code = 55 + Math.floor(Math.random()*36);
if (code < 65) code -= 7;
key += String.fromCharCode(code);
}
return key;
};

$(document).ready(function () {
$('.alert').hide();
$('#new_chat_form').submit(function (e) {
Expand All @@ -52,7 +62,7 @@
$('.alert').html(res.error).fadeIn(250);
}
else {
window.location = res.redirect;
window.location = res.redirect + '#' + generateKey();
}
});
});
Expand Down

0 comments on commit 0d92b71

Please sign in to comment.