PageRenderTime 101ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/esotericsoftware/kryo/serializers/BlowfishSerializer.java

https://gitlab.com/kidaa/kryo
Java | 82 lines | 51 code | 10 blank | 21 comment | 0 complexity | 28ccb1393dda50c669c87779c52d6250 MD5 | raw file
  1. /* Copyright (c) 2008, Nathan Sweet
  2. * All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
  5. * conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  8. * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
  9. * disclaimer in the documentation and/or other materials provided with the distribution.
  10. * - Neither the name of Esoteric Software nor the names of its contributors may be used to endorse or promote products derived
  11. * from this software without specific prior written permission.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
  14. * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  15. * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  16. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  17. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  18. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
  19. package com.esotericsoftware.kryo.serializers;
  20. import java.io.IOException;
  21. import javax.crypto.Cipher;
  22. import javax.crypto.CipherInputStream;
  23. import javax.crypto.CipherOutputStream;
  24. import javax.crypto.spec.SecretKeySpec;
  25. import com.esotericsoftware.kryo.Kryo;
  26. import com.esotericsoftware.kryo.KryoException;
  27. import com.esotericsoftware.kryo.Serializer;
  28. import com.esotericsoftware.kryo.io.Input;
  29. import com.esotericsoftware.kryo.io.Output;
  30. /** Encrypts data using the blowfish cipher.
  31. * @author Nathan Sweet <misc@n4te.com> */
  32. public class BlowfishSerializer extends Serializer {
  33. private final Serializer serializer;
  34. static private SecretKeySpec keySpec;
  35. public BlowfishSerializer (Serializer serializer, byte[] key) {
  36. this.serializer = serializer;
  37. keySpec = new SecretKeySpec(key, "Blowfish");
  38. }
  39. public void write (Kryo kryo, Output output, Object object) {
  40. Cipher cipher = getCipher(Cipher.ENCRYPT_MODE);
  41. CipherOutputStream cipherStream = new CipherOutputStream(output, cipher);
  42. Output cipherOutput = new Output(cipherStream, 256) {
  43. public void close () throws KryoException {
  44. // Don't allow the CipherOutputStream to close the output.
  45. }
  46. };
  47. serializer.write(kryo, cipherOutput, object);
  48. cipherOutput.flush();
  49. try {
  50. cipherStream.close();
  51. } catch (IOException ex) {
  52. throw new KryoException(ex);
  53. }
  54. }
  55. public Object read (Kryo kryo, Input input, Class type) {
  56. Cipher cipher = getCipher(Cipher.DECRYPT_MODE);
  57. CipherInputStream cipherInput = new CipherInputStream(input, cipher);
  58. return serializer.read(kryo, new Input(cipherInput, 256), type);
  59. }
  60. public Object copy (Kryo kryo, Object original) {
  61. return serializer.copy(kryo, original);
  62. }
  63. static private Cipher getCipher (int mode) {
  64. try {
  65. Cipher cipher = Cipher.getInstance("Blowfish");
  66. cipher.init(mode, keySpec);
  67. return cipher;
  68. } catch (Exception ex) {
  69. throw new KryoException(ex);
  70. }
  71. }
  72. }