mirror of
https://github.com/Tygs/0bin.git
synced 2023-08-10 21:13:00 +03:00
Decryption works
This commit is contained in:
parent
d1d455a41b
commit
d09e0feb1e
30
src/paste.py
30
src/paste.py
@ -29,7 +29,10 @@ class Paste(object):
|
|||||||
expiration=u'burn_after_reading',
|
expiration=u'burn_after_reading',
|
||||||
comments=None):
|
comments=None):
|
||||||
|
|
||||||
self.content = content.encode('utf8')
|
self.content = content
|
||||||
|
if isinstance(self.content, unicode):
|
||||||
|
self.content = self.content.encode('utf8')
|
||||||
|
|
||||||
self.uuid = uuid or hashlib.sha1(self.content).hexdigest()
|
self.uuid = uuid or hashlib.sha1(self.content).hexdigest()
|
||||||
self.expiration = self.get_expiration(expiration)
|
self.expiration = self.get_expiration(expiration)
|
||||||
self.comments = comments
|
self.comments = comments
|
||||||
@ -82,20 +85,18 @@ class Paste(object):
|
|||||||
try:
|
try:
|
||||||
paste = open(path)
|
paste = open(path)
|
||||||
uuid = os.path.basename(path)
|
uuid = os.path.basename(path)
|
||||||
expiration = paste.next()
|
expiration = paste.next().strip()
|
||||||
data = paste.next()
|
content = paste.next().strip()
|
||||||
comments = paste.read()[:-1] # remove the last coma
|
comments = paste.read()[:-1] # remove the last coma
|
||||||
|
|
||||||
if expiration != u'burn_after_reading':
|
if expiration != u'burn_after_reading':
|
||||||
expiration = datetime.strptime(expiration.strip(),
|
expiration = datetime.strptime(expiration,'%Y-%m-%d %H:%M:%S.%f')
|
||||||
'%Y-%m-%d %H:%M:%S.%f')
|
|
||||||
|
|
||||||
except StopIteration:
|
except StopIteration:
|
||||||
raise TypeError(u'File %s is malformed' % path)
|
raise TypeError(u'File %s is malformed' % path)
|
||||||
except (IOError, OSError):
|
except (IOError, OSError):
|
||||||
raise ValueError(u'Can not open paste from file %s' % path)
|
raise ValueError(u'Can not open paste from file %s' % path)
|
||||||
|
return Paste(uuid=uuid, comments=comments,
|
||||||
return Paste(uuid=uuid, comments=comments, expiration=expiration, **data)
|
expiration=expiration, content=content)
|
||||||
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@ -136,11 +137,14 @@ class Paste(object):
|
|||||||
os.mkdir(path)
|
os.mkdir(path)
|
||||||
self.DIR_CACHE.add((head, tail))
|
self.DIR_CACHE.add((head, tail))
|
||||||
|
|
||||||
with open(self.path, 'w') as f:
|
try:
|
||||||
f.write(unicode(self.expiration) + '\n')
|
with open(self.path, 'w') as f:
|
||||||
f.write(self.content + '\n')
|
f.write(unicode(self.expiration) + '\n')
|
||||||
if self.comments:
|
f.write(self.content + '\n')
|
||||||
f.write(comments)
|
if self.comments:
|
||||||
|
f.write(comments)
|
||||||
|
except:
|
||||||
|
import ipdb; ipdb.set_trace()
|
||||||
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
@ -16,7 +16,7 @@ font-size: 48px;
|
|||||||
line-height: 0;
|
line-height: 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
.brand p {
|
.brand em {
|
||||||
display: inline;
|
display: inline;
|
||||||
color: #D40202;
|
color: #D40202;
|
||||||
margin: 0px !important;
|
margin: 0px !important;
|
||||||
|
@ -2,24 +2,32 @@
|
|||||||
// Start random number generator seeding ASAP
|
// Start random number generator seeding ASAP
|
||||||
sjcl.random.startCollectors();
|
sjcl.random.startCollectors();
|
||||||
|
|
||||||
|
var zerobin = function() {
|
||||||
|
that = {};
|
||||||
|
that.base64 = {
|
||||||
|
decode: function(content) {
|
||||||
|
return sjcl.codec.utf8String.fromBits(sjcl.codec.base64.toBits(content));
|
||||||
|
},
|
||||||
|
encode: function(content) {
|
||||||
|
return sjcl.codec.base64.fromBits(sjcl.codec.utf8String.toBits(content));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
that.encrypt = function(key, content) {
|
||||||
|
var encrypted = sjcl.encrypt(key, content);
|
||||||
|
return lzw.compress(encrypted);
|
||||||
|
};
|
||||||
|
that.decrypt = function(key, content) {
|
||||||
|
var uncompressed = lzw.decompress(content)
|
||||||
|
return sjcl.decrypt(key, uncompressed);
|
||||||
|
};
|
||||||
|
that.make_key = function() {
|
||||||
|
return sjcl.codec.base64.fromBits(sjcl.random.randomWords(8, 0), 0);
|
||||||
|
};
|
||||||
|
return that;
|
||||||
|
}();
|
||||||
|
|
||||||
$(function(){
|
$(function(){
|
||||||
|
|
||||||
function encrypt(key, content) {
|
|
||||||
content = lzw.compress(sjcl.encrypt(key, content));
|
|
||||||
content = sjcl.codec.utf8String.toBits(content);
|
|
||||||
return sjcl.codec.base64.fromBits(content);
|
|
||||||
}
|
|
||||||
|
|
||||||
function decrypt(key, content) {
|
|
||||||
content = sjcl.codec.base64.toBits(content);
|
|
||||||
content = sjcl.codec.utf8String.fromBits(content);
|
|
||||||
return sjcl.decrypt(key, lzw.decompress(content));
|
|
||||||
}
|
|
||||||
|
|
||||||
function make_key() {
|
|
||||||
return sjcl.codec.base64.fromBits(sjcl.random.randomWords(8, 0), 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
$('button[type=submit]').click(function(e){
|
$('button[type=submit]').click(function(e){
|
||||||
|
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
@ -27,8 +35,8 @@ $('button[type=submit]').click(function(e){
|
|||||||
|
|
||||||
if (paste.trim()) {
|
if (paste.trim()) {
|
||||||
var expiration = $('#expiration').val();
|
var expiration = $('#expiration').val();
|
||||||
var key = make_key();
|
var key = zerobin.make_key();
|
||||||
var data = {content: encrypt(key, paste), expiration: expiration}
|
var data = {content: zerobin.encrypt(key, paste), expiration: expiration}
|
||||||
|
|
||||||
$.post('/paste/create', data)
|
$.post('/paste/create', data)
|
||||||
.error(function() {
|
.error(function() {
|
||||||
@ -42,5 +50,14 @@ $('button[type=submit]').click(function(e){
|
|||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
var content = $('#paste-content').text().trim();
|
||||||
|
var key = window.location.hash.substring(1);
|
||||||
|
if (content && key) {
|
||||||
|
try {
|
||||||
|
$('#paste-content').text(zerobin.decrypt(key, content));
|
||||||
|
} catch(err) {
|
||||||
|
alert('Could not decrypt data (Wrong key ?)');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
});
|
});
|
@ -34,7 +34,7 @@
|
|||||||
<span class="icon-bar"></span>
|
<span class="icon-bar"></span>
|
||||||
<span class="icon-bar"></span>
|
<span class="icon-bar"></span>
|
||||||
</a>
|
</a>
|
||||||
<a class="brand" href="#"><span>ø</span>bin<p>.net</p></a>
|
<a class="brand" href="/"><span>ø</span>bin<em>.net</em></a>
|
||||||
<div class="nav-collapse">
|
<div class="nav-collapse">
|
||||||
<ul class="nav">
|
<ul class="nav">
|
||||||
<li class="active"><a href="#">Home</a></li>
|
<li class="active"><a href="#">Home</a></li>
|
||||||
|
@ -1,59 +1,22 @@
|
|||||||
<form class="well" method="post" action="/paste/create">
|
<div class="well">
|
||||||
|
|
||||||
<ul>
|
<form action="/paste/clone" method="get" accept-charset="utf-8">
|
||||||
<li>
|
</form>
|
||||||
<span class="btn-group">
|
<p class="paste-option btn-group">
|
||||||
<button class="btn">New Paste</button>
|
<button class="btn">New Paste</button>
|
||||||
<button class="btn"><i class="icon-camera"></i> Clone</button>
|
<button class="btn"><i class="icon-camera"></i> Clone</button>
|
||||||
</span>
|
<p>
|
||||||
</li>
|
|
||||||
|
|
||||||
<li>
|
|
||||||
<span class="paste-option">
|
|
||||||
<label for='expire_in'>Expiration:</label>
|
|
||||||
<select id="expire_in" name="expire_in">
|
|
||||||
<option value="burn_after_reading">Burn after reading</option>
|
|
||||||
<option value="10_minutes">10 minutes</option>
|
|
||||||
<option value="1_hour">1 hour</option>
|
|
||||||
<option selected value="1_day">1 day</option>
|
|
||||||
<option value="1_month">1 month</option>
|
|
||||||
<option value="never">Never</option>
|
|
||||||
</select>
|
|
||||||
<button type="submit" class="btn btn-primary">Submit</button>
|
|
||||||
<span>
|
|
||||||
</li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
<textarea rows="10" style="width:100%;"
|
<pre id="paste-content">
|
||||||
class="input-xlarge"
|
{{ paste.content }}
|
||||||
id="content" name="content"></textarea>
|
</pre>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
<p class="paste-option btn-group">
|
||||||
<ul>
|
<button class="btn">New Paste</button>
|
||||||
<li>
|
<button class="btn"><i class="icon-camera"></i> Clone</button>
|
||||||
<span class="btn-group">
|
<p>
|
||||||
<button class="btn">New Paste</button>
|
|
||||||
<button class="btn"><i class="icon-camera"></i> Clone</button>
|
|
||||||
</span>
|
|
||||||
</li>
|
|
||||||
|
|
||||||
<li>
|
|
||||||
<span class="paste-option">
|
|
||||||
<label >Expiration:</label>
|
|
||||||
<select id="expire_in" name="expire_in">
|
|
||||||
<option value="burn_after_reading">Burn after reading</option>
|
|
||||||
<option value="10_minutes">10 minutes</option>
|
|
||||||
<option value="1_hour">1 hour</option>
|
|
||||||
<option selected value="1_day">1 day</option>
|
|
||||||
<option value="1_month">1 month</option>
|
|
||||||
<option value="never">Never</option>
|
|
||||||
</select>
|
|
||||||
<button type="submit" class="btn btn-primary">Submit</button>
|
|
||||||
<span>
|
|
||||||
</li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user