annotate src/plugins/plugin_xep_0300.py @ 1549:e987325c14ef

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