项目作者: codesqueak

项目描述 :
Jackson Crypto Extension Module
高级语言: Java
项目地址: git://github.com/codesqueak/jackson-json-crypto.git
创建时间: 2017-12-28T20:11:48Z
项目社区:https://github.com/codesqueak/jackson-json-crypto

开源协议:MIT License

下载


CodeQL
Java CI with Gradle
Maven Central

Jackson JSON Crypto Module

A Jackson module to support JSON encryption and decryption operations. Encryption is via AES. Key generation is password
based.

Keyword: Jackson, JSON, AES, PKCS5, Encryption, Salt, Initialization Vector, IV, Java

Based on an idea from meltmedia

If you find this project useful, you may want to Buy me a Coffee! :coffee: Thanks :thumbsup:

Build

Windows

  1. gradlew clean build test

Linux

  1. ./gradlew clean build test

How to use

These examples are demonstrated in the CryptoDemo unit test class

Option 1 - Very Quick and Easy

Add the crypto module to your project. Common use case with a cipher of AES/CBC/PKCS5Padding and a key factory algorithm of PBKDF2WithHmacSHA512

Just supply a password.

  1. ObjectMapper objectMapper = EncryptionService.getInstance("Password1");

Option 2 - Quick and Easy

Similar to Option 1, but you already have a ObjectMapper

  1. ObjectMapper objectMapper = ...
  2. EncryptionService encryptionService =
  3. new EncryptionService(objectMapper, new PasswordCryptoContext("Password1");
  4. objectMapper.registerModule(new CryptoModule().addEncryptionService(encryptionService));

Option 3 - Configure Everything

Where you just need full control.

  1. // get an object mapper
  2. ObjectMapper objectMapper = new ObjectMapper();
  3. // set up a custom crypto context - Defines the interface to the crypto algorithms used
  4. ICryptoContext cryptoContext = new PasswordCryptoContext("Password");
  5. // The encryption service holds functionality to map clear to / from encrypted JSON
  6. EncryptionService encryptionService = new EncryptionService(objectMapper, cryptoContext);
  7. // Create a Jackson module and tell it about the encryption service
  8. CryptoModule cryptoModule = new CryptoModule().addEncryptionService(encryptionService);
  9. // Tell Jackson about the new module
  10. objectMapper.registerModule(cryptoModule);

Encrypt a field

Any field that is required to be encrypted has to be marked as such. This can be done by either annotating the getter() or
by annotating the field definition.

This …

  1. private String critical;
  2. ...
  3. @JsonProperty
  4. @Encrypt
  5. public String getCritical() {
  6. return this.critical;
  7. }

… or …

  1. @JsonProperty
  2. @Encrypt
  3. private String critical;

Output JSON Format

  1. {
  2. "critical":{
  3. "salt":"IRqsz99no75sx9SCGrzOSEdoMVw=",
  4. "iv":"bfKvxBhq7X5su9VtvDdOGQ==",
  5. "value":"pXWsFPzCnmPieitbGfkvofeQE3fj0Kb4mSP7e28+Jc0="
  6. }
  7. }

Using Jenkins

The project includes a Jenkins file to control a pipeline build.

Include Using Maven

  1. <dependency>
  2. <groupId>com.codingrodent</groupId>
  3. <artifactId>jackson-json-crypto</artifactId>
  4. <version>2.2.0</version>
  5. </dependency>

Include Using Gradle

  1. compile group: 'com.codingrodent', name: 'jackson-json-crypto', version: '2.2.0'