PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/Contents/Libraries/Shared/Crypto/Cipher/Blowfish.py

https://gitlab.com/sisfs/G_Music
Python | 121 lines | 83 code | 0 blank | 38 comment | 0 complexity | a05c42d802329888ac6f5391ab223360 MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Cipher/Blowfish.py : Blowfish
  4. #
  5. # ===================================================================
  6. # The contents of this file are dedicated to the public domain. To
  7. # the extent that dedication to the public domain is not available,
  8. # everyone is granted a worldwide, perpetual, royalty-free,
  9. # non-exclusive license to exercise all rights associated with the
  10. # contents of this file for any purpose whatsoever.
  11. # No rights are reserved.
  12. #
  13. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  17. # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  18. # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. # SOFTWARE.
  21. # ===================================================================
  22. """Blowfish symmetric cipher
  23. Blowfish_ is a symmetric block cipher designed by Bruce Schneier.
  24. It has a fixed data block size of 8 bytes and its keys can vary in length
  25. from 32 to 448 bits (4 to 56 bytes).
  26. Blowfish is deemed secure and it is fast. However, its keys should be chosen
  27. to be big enough to withstand a brute force attack (e.g. at least 16 bytes).
  28. As an example, encryption can be done as follows:
  29. >>> from Crypto.Cipher import Blowfish
  30. >>> from Crypto import Random
  31. >>> from struct import pack
  32. >>>
  33. >>> bs = Blowfish.block_size
  34. >>> key = b'An arbitrarily long key'
  35. >>> iv = Random.new().read(bs)
  36. >>> cipher = Blowfish.new(key, Blowfish.MODE_CBC, iv)
  37. >>> plaintext = b'docendo discimus '
  38. >>> plen = bs - divmod(len(plaintext),bs)[1]
  39. >>> padding = [plen]*plen
  40. >>> padding = pack('b'*plen, *padding)
  41. >>> msg = iv + cipher.encrypt(plaintext + padding)
  42. .. _Blowfish: http://www.schneier.com/blowfish.html
  43. :undocumented: __revision__, __package__
  44. """
  45. __revision__ = "$Id$"
  46. from Crypto.Cipher import blockalgo
  47. from Crypto.Cipher import _Blowfish
  48. class BlowfishCipher (blockalgo.BlockAlgo):
  49. """Blowfish cipher object"""
  50. def __init__(self, key, *args, **kwargs):
  51. """Initialize a Blowfish cipher object
  52. See also `new()` at the module level."""
  53. blockalgo.BlockAlgo.__init__(self, _Blowfish, key, *args, **kwargs)
  54. def new(key, *args, **kwargs):
  55. """Create a new Blowfish cipher
  56. :Parameters:
  57. key : byte string
  58. The secret key to use in the symmetric cipher.
  59. Its length can vary from 4 to 56 bytes.
  60. :Keywords:
  61. mode : a *MODE_** constant
  62. The chaining mode to use for encryption or decryption.
  63. Default is `MODE_ECB`.
  64. IV : byte string
  65. The initialization vector to use for encryption or decryption.
  66. It is ignored for `MODE_ECB` and `MODE_CTR`.
  67. For `MODE_OPENPGP`, IV must be `block_size` bytes long for encryption
  68. and `block_size` +2 bytes for decryption (in the latter case, it is
  69. actually the *encrypted* IV which was prefixed to the ciphertext).
  70. It is mandatory.
  71. For all other modes, it must be `block_size` bytes longs. It is optional and
  72. when not present it will be given a default value of all zeroes.
  73. counter : callable
  74. (*Only* `MODE_CTR`). A stateful function that returns the next
  75. *counter block*, which is a byte string of `block_size` bytes.
  76. For better performance, use `Crypto.Util.Counter`.
  77. segment_size : integer
  78. (*Only* `MODE_CFB`).The number of bits the plaintext and ciphertext
  79. are segmented in.
  80. It must be a multiple of 8. If 0 or not specified, it will be assumed to be 8.
  81. :Return: a `BlowfishCipher` object
  82. """
  83. return BlowfishCipher(key, *args, **kwargs)
  84. #: Electronic Code Book (ECB). See `blockalgo.MODE_ECB`.
  85. MODE_ECB = 1
  86. #: Cipher-Block Chaining (CBC). See `blockalgo.MODE_CBC`.
  87. MODE_CBC = 2
  88. #: Cipher FeedBack (CFB). See `blockalgo.MODE_CFB`.
  89. MODE_CFB = 3
  90. #: This mode should not be used.
  91. MODE_PGP = 4
  92. #: Output FeedBack (OFB). See `blockalgo.MODE_OFB`.
  93. MODE_OFB = 5
  94. #: CounTer Mode (CTR). See `blockalgo.MODE_CTR`.
  95. MODE_CTR = 6
  96. #: OpenPGP Mode. See `blockalgo.MODE_OPENPGP`.
  97. MODE_OPENPGP = 7
  98. #: Size of a data block (in bytes)
  99. block_size = 8
  100. #: Size of a key (in bytes)
  101. key_size = xrange(4,56+1)