Code Monkey home page Code Monkey logo

delightful-anonymization's Introduction

Build Status Maven Central

delightful-anonymization is a library for anonymizing case classes on-the-fly.

This library is built for Scala 2.12 and 2.13.

SBT

libraryDependencies += "org.sweet-delights" %% "delightful-anonymization" % "0.1.1"

Maven

<dependency>
  <groupId>org.sweet-delights</groupId>
  <artifactId>delightful-anonymization_2.12</artifactId>
  <version>0.0.1</version>
</dependency>

All files in delightful-anonymization are under the GNU Lesser General Public License version 3. Please read files COPYING and COPYING.LESSER for details.

How to anonymize a case class ?

Step 1: decorate a case class with @PII annotations. Example:

import sweet.delights.anonymization.{Hash, PII}

case class Foo(
  opt: Option[String] @PII(Hash.MD5),
  str: String         @PII(Hash.SHA512),
  integer: Int
)

Step 2: apply the anonymize function on an instance of Foo:

val foo = Foo(
  Some("opt"),
  "str",
  1
)

val anonymized == Foo(
opt = Some("@-A9WeZjwa+awzqZSdEZNQWg==")
,
str = "@-Ms3snktf//qQkCS0pxCFDuLhtNPxn/2PJImMPoQBmZes+h+d3Q39yiEojcksp2agyxDgzXstaSbe/+zMWSOVAg=="
,
integer = 1
)
//> true

Supported types

By default, Anonymizer hashes arrays of bytes. But any type T - other than products and co-products - that can be transformed into an array of bytes can be hashed.

The support for additional types is done via Injections, a mechanism borrowed from the frameless library.

For example, support for strings is added with the following:

import org.apache.commons.codec.binary.Base64
import sweet.delights.anonymization.Injection

lazy val anonymizedPrefix = "@:"

implicit lazy val stringInjection: Injection[String, Array[Byte]] = new Injection[String, Array[Byte]] {
  override def isAnonymized(t: String): Boolean = t.startsWith(anonymizedPrefix)

  override def apply(t: String): Array[Byte] = t.getBytes("UTF-8")

  override def invert(u: Array[Byte]): String = anonymizedPrefix + Base64.encodeBase64String(u)
}

Comments:

  • idempotence is achieved by calling the isAnonymized function. If it returns true then the value t is not re-hashed. Otherwise the specified hashing algorithm is applied.
  • it is up to the user to decide which injections are to be idempotent or not
  • the default hashing implementation of strings is idempotent

Supported hashing algorithms

The hashing algorithms are those supported by Java 8:

  • MD5
  • SHA-1
  • SHA-256
  • SHA-384
  • SHA-512

Other algorithms, not necessarly hashing algorithms, could be implemented. For instance, the anonymization method FirstLetter-of-a-string could be added. Contributions welcome!

Acknowledgments

delightful-anonymization's People

Contributors

pgrandjean avatar scala-steward avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

scala-steward

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.