Skip to content

Instantly share code, notes, and snippets.

@itsmikita
Last active September 3, 2015 14:19
Show Gist options
  • Save itsmikita/792ec5ede0abae45ed96 to your computer and use it in GitHub Desktop.
Save itsmikita/792ec5ede0abae45ed96 to your computer and use it in GitHub Desktop.
Console (Anonymous) Chat with encryption
/**
* This script requires jQuery to be included.
* TODO: Fix non-jQuery or insert jQuery.
*
* It also encrypts messages with your key so that only
* you and people you shared the key via other medias like
* Facebook or phone can read the messages.
*/
// Help (:
function h() {
console.log( "C H A T H E L P" );
console.log( "l( <username>, <key> ) --- Login into chat with your own secret key, only people who have this key will receive the messages (and also only ones to read it)" );
console.log( "m( <message> ) --- Post a message" );
console.log( "x() --- Log out, exit the chat (NOTE: clears the log)." );
console.log( "h() — Prints this help menu." );
}
// Login
function l( username, key ) {
window.__u = name;
window.__k = key;
window.__c = setInterval( function() {
u();
}, 5000 );
}
// Update
function u() {
$.ajax( {
url: ‘/ajax.php’,
data: {
t: window.__t;
},
type: ‘POST’,
success: function( response ) {
r( response );
},
} );
}
// Message
function m( text ) {
$.ajax( {
url: ‘/ajax.php’,
data: {
m: e( text ),
t: window.__t,
},
type: ‘POST’,
success: function( response ) {
r( response );
},
} );
};
// Encypt
function e( text ) {
text = window.btoa( window.btoa( window.__k ) + window.btoa( text ) );
return text;
}
// Decrypt
function d( text ) {
text = window.atob( window.atob( text ).replace( window.btoa( window.__k ), '' ) ) );
return text;
}
// Response
function r( response ) {
if( ! response.messages.length )
return;
for( var i in response.messages )
console.log( response.messages[ i ].username + ‘: ‘ + d( response.messages[ i ].message ) );
window.__t = this.response.time;
};
// Exit
function x() {
claerInterval( window.__c );
window.__n = null;
window.__k = null;
console.log( 'You left. Clearing log in 3 seconds...' );
// Clear console log
setTimeout( function() {
console.clear();
}, 3000 );
}
<?php
/**
* This script requires a MySQL database connection
* with the mapped table set properly. See database
* table structure.
*/
// Instance a DB connection here
$db = '';
// Unix timestamp it is
$time = intval( $_POST['t'] );
// Do escape the data !!!
$username = $db->escape( $_POST['u'] );
// If there's something to post
if( $_POST['m'] ) {
$message = $db->escape( $_POST['m'] );
$db->query( "INSERT INTO c ( user, time, message ) VALUES ( '$user', '$time', '$message' );" );
}
$messages = $db->get_results( "SELECT user, time, message FROM c WHERE time >= '$time';" );
$time = time();
print( json_encode( compact( 'messages', 'time' ) );
CREATE TABLE `c` (
`user` varchar(255) DEFAULT NULL,
`message` int(255) DEFAULT NULL,
`time` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
@Ashtor
Copy link

Ashtor commented Apr 1, 2015

hi. a quick question i need answer on. this "Console (Anonymous) Chat with encryption" .. is it a real chatsite for this? and if so.. whats the chatsite name for it?

@itsmikita
Copy link
Author

@Ashtor: Nix, this is just an idea, a concept of using web browser console as encrypted chat :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment