Mercurial > libervia-backend
comparison src/plugins/plugin_xep_0300.py @ 1526:bb451fd1cea3
plugin XEP-0300: hashes management first draft
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 25 Sep 2015 19:19:12 +0200 |
parents | |
children | 0de5f210fe56 |
comparison
equal
deleted
inserted
replaced
1525:49d33cb48207 | 1526:bb451fd1cea3 |
---|---|
1 #!/usr/bin/python | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # SAT plugin for Hash functions (XEP-0300) | |
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Jérôme Poisson (goffi@goffi.org) | |
6 | |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 from sat.core.i18n import _ | |
21 from sat.core.log import getLogger | |
22 log = getLogger(__name__) | |
23 from twisted.words.xish import domish | |
24 import hashlib | |
25 | |
26 | |
27 NS_HASHES = "urn:xmpp:hashes:1" | |
28 | |
29 PLUGIN_INFO = { | |
30 "name": "Cryptographic Hash Functions", | |
31 "import_name": "XEP-0300", | |
32 "type": "XEP", | |
33 "protocols": ["XEP-0300"], | |
34 "main": "XEP_0300", | |
35 "handler": "no", | |
36 "description": _("""Management of cryptographic hashes""") | |
37 } | |
38 | |
39 | |
40 class XEP_0300(object): | |
41 ALGOS = {u'md5': hashlib.md5, | |
42 u'sha-1': hashlib.sha1, | |
43 u'sha-256': hashlib.sha256, | |
44 u'sha-512': hashlib.sha512, | |
45 } | |
46 | |
47 def __init__(self, host): | |
48 log.info(_("plugin Hashes initialization")) | |
49 | |
50 def buidHash(self, file_obj=None, algo='sha-256'): | |
51 """Compute hash and build hash element | |
52 | |
53 @param file_obj(file, None): file to use to calculate the hash | |
54 if file_obj is None, en empty hash element will be returned (useful e.g. in XEP-0234) | |
55 @param algo(unicode): algorithme to use, must be a key of XEP_0300.ALGOS | |
56 @return (domish.Element): computed hash | |
57 """ | |
58 hasher = self.ALGOS[algo] | |
59 hash_elt = domish.Element((NS_HASHES, 'hash')) | |
60 hash_elt['algo']=algo | |
61 # TODO: actually hash, use deferToThread | |
62 return hash_elt | |
63 |