Skip to content

Commit

Permalink
Helidon crypto module (#2989)
Browse files Browse the repository at this point in the history
Cryptography module created

Signed-off-by: David Kral <david.k.kral@oracle.com>
  • Loading branch information
Verdent authored Jun 9, 2021
1 parent ce28e04 commit 241d8aa
Show file tree
Hide file tree
Showing 42 changed files with 2,476 additions and 43 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

For Helidon 1.x releases please see [Helidon 1.x CHANGELOG.md](https://github.com/oracle/helidon/blob/helidon-1.x/CHANGELOG.md)

## [2.3.1-SNAPSHOT]
### Compatibility
Base64Value has been moved from `Helidon Integrations Common REST` module to the module `Helidon Common`. Due to that action,
import has changed from `io.helidon.integrations.common.rest.Base64Value` to `io.helidon.common.Base64Value`, but the class is
the same.

## [2.3.0-SNAPSHOT]

2.3.0 is a minor release of Helidon that contains bug fixes and enhancements. Notable enhancements:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package io.helidon.integrations.common.rest;
package io.helidon.common;

import java.nio.charset.StandardCharsets;
import java.util.Base64;
Expand All @@ -33,10 +33,27 @@ public class Base64Value {
private static final Base64.Encoder ENCODER = Base64.getEncoder();
private static final Base64.Decoder DECODER = Base64.getDecoder();

private final String base64;
private final LazyValue<String> base64;
private final LazyValue<byte[]> bytes;
private final LazyValue<String> plainString;

private Base64Value(String base64) {
this.base64 = base64;
this.base64 = LazyValue.create(base64);
this.bytes = LazyValue.create(() -> DECODER.decode(base64));
this.plainString = LazyValue.create(() -> new String(this.bytes.get(), StandardCharsets.UTF_8));
}

private Base64Value(byte[] bytes) {
this.bytes = LazyValue.create(bytes);
this.base64 = LazyValue.create(() -> ENCODER.encodeToString(bytes));
this.plainString = LazyValue.create(() -> new String(bytes, StandardCharsets.UTF_8));
}

@SuppressWarnings("unused")
private Base64Value(String plainString, boolean ignored) {
this.plainString = LazyValue.create(plainString);
this.bytes = LazyValue.create(() -> plainString.getBytes(StandardCharsets.UTF_8));
this.base64 = LazyValue.create(() -> ENCODER.encodeToString(this.bytes.get()));
}

/**
Expand All @@ -47,7 +64,7 @@ private Base64Value(String base64) {
* @return a new value
*/
public static Base64Value create(String plainText) {
return create(plainText.getBytes(StandardCharsets.UTF_8));
return new Base64Value(plainText, true);
}

/**
Expand All @@ -57,7 +74,7 @@ public static Base64Value create(String plainText) {
* @return a new value
*/
public static Base64Value create(byte[] bytes) {
return createFromEncoded(ENCODER.encodeToString(bytes));
return new Base64Value(bytes);
}

/**
Expand All @@ -76,7 +93,7 @@ public static Base64Value createFromEncoded(String base64Text) {
* @return base64 value
*/
public String toBase64() {
return base64;
return base64.get();
}

/**
Expand All @@ -85,7 +102,7 @@ public String toBase64() {
* @return bytes
*/
public byte[] toBytes() {
return DECODER.decode(base64);
return bytes.get();
}

/**
Expand All @@ -98,11 +115,11 @@ public byte[] toBytes() {
* @return string value from the decoded bytes
*/
public String toDecodedString() {
return new String(toBytes(), StandardCharsets.UTF_8);
return plainString.get();
}

@Override
public String toString() {
return base64;
return base64.get();
}
}
33 changes: 33 additions & 0 deletions common/crypto/etc/spotbugs/exclude.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2021 Oracle and/or its affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<FindBugsFilter
xmlns="https://github.com/spotbugs/filter/3.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://github.com/spotbugs/filter/3.0.0 https://raw.githubusercontent.com/spotbugs/spotbugs/3.1.0/spotbugs/etc/findbugsfilter.xsd">

<!--
Initialization vector is not static.
-->
<Match>
<Class name="io.helidon.common.crypto.SymmetricCipher"/>
<Method name="createAlgorithmParameter" />
<Bug pattern="STATIC_IV" />
</Match>
</FindBugsFilter>
57 changes: 57 additions & 0 deletions common/crypto/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2021 Oracle and/or its affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>io.helidon.common</groupId>
<artifactId>helidon-common-project</artifactId>
<version>2.3.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>helidon-common-crypto</artifactId>
<name>Helidon Common Crypto</name>

<properties>
<spotbugs.exclude>etc/spotbugs/exclude.xml</spotbugs.exclude>
</properties>

<dependencies>
<dependency>
<groupId>io.helidon.common</groupId>
<artifactId>helidon-common</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Loading

0 comments on commit 241d8aa

Please sign in to comment.