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',
|
||||
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.expiration = self.get_expiration(expiration)
|
||||
self.comments = comments
|
||||
@ -82,20 +85,18 @@ class Paste(object):
|
||||
try:
|
||||
paste = open(path)
|
||||
uuid = os.path.basename(path)
|
||||
expiration = paste.next()
|
||||
data = paste.next()
|
||||
expiration = paste.next().strip()
|
||||
content = paste.next().strip()
|
||||
comments = paste.read()[:-1] # remove the last coma
|
||||
|
||||
if expiration != u'burn_after_reading':
|
||||
expiration = datetime.strptime(expiration.strip(),
|
||||
'%Y-%m-%d %H:%M:%S.%f')
|
||||
expiration = datetime.strptime(expiration,'%Y-%m-%d %H:%M:%S.%f')
|
||||
|
||||
except StopIteration:
|
||||
raise TypeError(u'File %s is malformed' % path)
|
||||
except (IOError, OSError):
|
||||
raise ValueError(u'Can not open paste from file %s' % path)
|
||||
|
||||
return Paste(uuid=uuid, comments=comments, expiration=expiration, **data)
|
||||
return Paste(uuid=uuid, comments=comments,
|
||||
expiration=expiration, content=content)
|
||||
|
||||
|
||||
@classmethod
|
||||
@ -136,11 +137,14 @@ class Paste(object):
|
||||
os.mkdir(path)
|
||||
self.DIR_CACHE.add((head, tail))
|
||||
|
||||
with open(self.path, 'w') as f:
|
||||
f.write(unicode(self.expiration) + '\n')
|
||||
f.write(self.content + '\n')
|
||||
if self.comments:
|
||||
f.write(comments)
|
||||
try:
|
||||
with open(self.path, 'w') as f:
|
||||
f.write(unicode(self.expiration) + '\n')
|
||||
f.write(self.content + '\n')
|
||||
if self.comments:
|
||||
f.write(comments)
|
||||
except:
|
||||
import ipdb; ipdb.set_trace()
|
||||
|
||||
return self
|
||||
|
||||
|
@ -16,7 +16,7 @@ font-size: 48px;
|
||||
line-height: 0;
|
||||
|
||||
}
|
||||
.brand p {
|
||||
.brand em {
|
||||
display: inline;
|
||||
color: #D40202;
|
||||
margin: 0px !important;
|
||||
|
@ -2,24 +2,32 @@
|
||||
// Start random number generator seeding ASAP
|
||||
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 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){
|
||||
|
||||
e.preventDefault();
|
||||
@ -27,8 +35,8 @@ $('button[type=submit]').click(function(e){
|
||||
|
||||
if (paste.trim()) {
|
||||
var expiration = $('#expiration').val();
|
||||
var key = make_key();
|
||||
var data = {content: encrypt(key, paste), expiration: expiration}
|
||||
var key = zerobin.make_key();
|
||||
var data = {content: zerobin.encrypt(key, paste), expiration: expiration}
|
||||
|
||||
$.post('/paste/create', data)
|
||||
.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>
|
||||
</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">
|
||||
<ul class="nav">
|
||||
<li class="active"><a href="#">Home</a></li>
|
||||
|
@ -1,59 +1,22 @@
|
||||
<form class="well" method="post" action="/paste/create">
|
||||
<div class="well">
|
||||
|
||||
<ul>
|
||||
<li>
|
||||
<span class="btn-group">
|
||||
<button class="btn">New Paste</button>
|
||||
<button class="btn"><i class="icon-camera"></i> Clone</button>
|
||||
</span>
|
||||
</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>
|
||||
<form action="/paste/clone" method="get" accept-charset="utf-8">
|
||||
</form>
|
||||
<p class="paste-option btn-group">
|
||||
<button class="btn">New Paste</button>
|
||||
<button class="btn"><i class="icon-camera"></i> Clone</button>
|
||||
<p>
|
||||
|
||||
<p>
|
||||
<textarea rows="10" style="width:100%;"
|
||||
class="input-xlarge"
|
||||
id="content" name="content"></textarea>
|
||||
<pre id="paste-content">
|
||||
{{ paste.content }}
|
||||
</pre>
|
||||
</p>
|
||||
|
||||
|
||||
<ul>
|
||||
<li>
|
||||
<span class="btn-group">
|
||||
<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>
|
||||
<p class="paste-option btn-group">
|
||||
<button class="btn">New Paste</button>
|
||||
<button class="btn"><i class="icon-camera"></i> Clone</button>
|
||||
<p>
|
||||
|
||||
</form>
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user