Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
nykim0402 committed Jul 17, 2020
1 parent 150137b commit 686be23
Show file tree
Hide file tree
Showing 9 changed files with 618 additions and 0 deletions.
87 changes: 87 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# pallycon-wm-api-request-sample

This sample is to get specification for PallyCon Watermark APIs for java
<br><br>

## Prerequisites

### IDE

- JAVA JDK 1.8 +

<br><br>


## Quick Start

**FOLLOW THE TODOs**

This code is from `src\test\java\com\pallycon\sample\WatermarkApiImplTest.java` Class.

```java

public class WatermarkApiImplTest {
private static Logger logger = LoggerFactory.getLogger(WatermarkApiImplTest.class);
WatermarkApi watermarkApi;

@Before
public void setUp() {
// TODO 1st: set these variables below.
String siteId = "TUTO";
String accessKey = "LT2FVJDp2Xr018zf4Di6lzvNOv3DKP20";
String siteKey = "lU5D8s3PWoLls3PWFWkClULlFWk5D8oC";

// TODO 2nd: construct WatermarkApiImpl.
watermarkApi = new WatermarkApiImpl(siteId, accessKey, siteKey);
}

@Test
public void execute() throws Exception {
// TODO 3rd: set jsonRequest.
String jsonRequest = "{\n" +
" \"storage_type\": \"S3\",\n" +
" \"region\": \"RG011\"\n" +
"}";

// TODO 4th: call method `execute`.
String result = watermarkApi.execute(jsonRequest);

logger.info("json Data : " + jsonRequest);
logger.info("data : " + watermarkApi.getData());
logger.info("hash : " + watermarkApi.getHash());
logger.info("timestamp : " + watermarkApi.getTimestamp());
logger.info("result : " + result);
}
}
```
<br>

#### Result of quick start

```
[main] INFO WatermarkApiImplTest - json Data : {
"storage_type": "S3",
"region": "RG011"
}
[main] INFO WatermarkApiImplTest - data : vvl3SpynNOShxsNa3cBOomYzaKX3+7JQ5ohZ4aqOFJFAUotKeUoV0wYN6nUu7lrdiFW3OCgGiIHDe1/BvdQhOQ==
[main] INFO WatermarkApiImplTest - hash : 38MDPO7fkXhRxmaLISVMy0UCZ1ExScIT7b33z6E+S7E=
[main] INFO WatermarkApiImplTest - timestamp : 2020-07-15T10:42:34Z
[main] INFO WatermarkApiImplTest - result : eyJkYXRhIjoidnZsM1NweW5OT1NoeHNOYTNjQk9vbVl6YUtYMys3SlE1b2haNGFxT0ZKRkFVb3RLZVVvVjB3WU42blV1N2xyZGlGVzNPQ2dHaUlIRGUxXC9CdmRRaE9RPT0iLCJoYXNoIjoiMzhNRFBPN2ZrWGhSeG1hTElTVk15MFVDWjFFeFNjSVQ3YjMzejZFK1M3RT0iLCJ0aW1lc3RhbXAiOiIyMDIwLTA3LTE1VDEwOjQyOjM0WiJ9
```


<br><br>

### HOW IT WORKS ?

If want to see how it works, go to Class `src\main\java\com\pallycon\sample\watermark\WatermarkApiImpl.java`. On the top of this Class, there is a simple note how it works.

> 1. get `site_id`, `access_key`, `site_key`, and `json_req`
> 2. encrypt `json_req` with `_make_data()`.
> 3. make `timestamp` with `_make_timestamp()`.
> 4. make `hash` with `_make_hash()`.
> 5. return PallyCon Watermark API specificated String from the values `#2 ~ #4`
<br><br><br><br><br><br>

60 changes: 60 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.pallycon</groupId>
<artifactId>sample</artifactId>
<version>1.0.0</version>
<name>sample</name>
<description>PallyCon Java Sample Project for Watermark API</description>

<properties>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk16</artifactId>
<version>1.46</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

</project>
31 changes: 31 additions & 0 deletions src/main/java/com/pallycon/sample/config/ApiUrl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.pallycon.sample.config;

/**
* Enum Class for API URL and METHOD
*/
public enum ApiUrl {
PACK_JOB_REGISTER("https://api.pallycon.com/api/v2/pack/", "POST"),
PACK_JOB_LIST("https://api.pallycon.com/api/v2/pack/", "GET"),
SESSION_WATERMARK_URL("https://watermark.pallycon.com/api/v2/session/watermarkUrl/", "GET"),
SESSION_WATERMARK_URL_AKAMAI("https://watermark.pallycon.com/api/v2/session/watermarkUrl/akamai/", "GET"),
STORAGE_REGISTER("https://api.pallycon.com/api/v2/storage/", "POST"),
STORAGE_UPDATE("https://api.pallycon.com/api/v2/storage/", "PUT"),
STORAGE_LIST("https://api.pallycon.com/api/v2/storage/", "GET")
;

private String url;
private String method;

ApiUrl(String url, String method) {
this.url = url;
this.method = method;
}

public String getUrl() {
return url;
}

public String getMethod() {
return method;
}
}
101 changes: 101 additions & 0 deletions src/main/java/com/pallycon/sample/util/Base64Encoder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.pallycon.sample.util;
/*
* Description :
*
* This file contains a static class which handles Base64 encoding.
*
* Since :
*
* 2007.10.20
*
* Author :
*
* JO Hyeong-ryeol (http://www.hyeongryeol.com/6)
*
* Copyright :
*
* Permission to copy, use, modify, sell and distribute this software is granted provided this
* copyright notice appears in all copies. This software is provided "as is" without express
* or implied warranty, and with no claim as to its suitability for any purpose.
*
* Copyright (C) 2007 by JO Hyeong-ryeol.
*
* $Id: Base64Encoder.java 59 2007-12-12 12:32:33Z JO Hyeong-ryeol $
*
*/

/**
* This static class handles Base64 encoding.
*
* @author JO Hyeong-ryeol
*/
public final class Base64Encoder {
private static final char BASE64_PADDING = '=';
private static final String BASE64_CHARS
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
private static final String BASE64__URL_CHARS
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";

private Base64Encoder() {
}

/**
* Converts a binary data into a Base64 encoded string.
* If the binary data is null or a zero-length array, an empty string is returned.
*
* @param value A byte array to encode.
* If it is null or a zero-length array, an empty string is returned.
* @return A converted Base64 string.
*/
public static String encode(byte[] value) {
if (value == null || value.length == 0)
return "";

// Get a required number of characters.
int requiredSize = (value.length / 3) * 4;
if (value.length % 3 > 0)
requiredSize += 4;

// Allocate memory
char[] output = new char[requiredSize];

// Converts the byte array into a Base64 string. (3 bytes to 4 characters)
int outputIndex = 0, bufferIndex = 0;
int[] buffer = new int[3];
int base64CharIndex;

for (int i = 0; i < value.length; i++) {
// Accumulate bytes in buffer.
buffer[bufferIndex++] = 0xFF & value[i];

// If 3 bytes are collected or the last byte is collected
if (bufferIndex == 3 || i == (value.length - 1)) {
// Make a first character
base64CharIndex = buffer[0] >>> 2;
output[outputIndex++] = BASE64_CHARS.charAt(base64CharIndex);

// Make a second character
base64CharIndex = ((buffer[0] & 0x03) << 4) | (buffer[1] >>> 4);
output[outputIndex++] = BASE64_CHARS.charAt(base64CharIndex);

// Make a third character
base64CharIndex = ((buffer[1] & 0x0F) << 2) | (buffer[2] >>> 6);
output[outputIndex++] = bufferIndex > 1 ? BASE64_CHARS
.charAt(base64CharIndex) : BASE64_PADDING;

// Make a fourth character
base64CharIndex = buffer[2] & 0x3F;
output[outputIndex++] = bufferIndex > 2 ? BASE64_CHARS
.charAt(base64CharIndex) : BASE64_PADDING;

// Reset variables related to the buffer
bufferIndex = 0;
for (int j = 0; j < buffer.length; j++)
buffer[j] = 0;
}
}

// Return as a string
return new String(output);
}
}
110 changes: 110 additions & 0 deletions src/main/java/com/pallycon/sample/util/StringEncrypter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.pallycon.sample.util;
/*
* Description :
*
* This file contains a class which converts a UTF-8 string into a cipher string, and vice versa.
* The class uses 128-bit AES Algorithm in Cipher Block Chaining (CBC) mode with a UTF-8 key
* string and a UTF-8 initial vector string which are hashed by MD5. PKCS5Padding is used
* as a padding mode and binary output is encoded by Base64.
*
* Since :
*
* 2007.10.20
*
* Author :
*
* JO Hyeong-ryeol (http://www.hyeongryeol.com/6)
*
* Copyright :
*
* Permission to copy, use, modify, sell and distribute this software is granted provided this
* copyright notice appears in all copies. This software is provided "as is" without express
* or implied warranty, and with no claim as to its suitability for any purpose.
*
* Copyright (C) 2007 by JO Hyeong-ryeol.
*
* $Id: StringEncrypter.java 65 2007-12-14 15:29:49Z JO Hyeong-ryeol $
*
*/

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.Security;
import java.util.Random;

/**
* This class converts a UTF-8 string into a cipher string, and vice versa.
* It uses 128-bit AES Algorithm in Cipher Block Chaining (CBC) mode with a UTF-8 key
* string and a UTF-8 initial vector string which are hashed by MD5. PKCS5Padding is used
* as a padding mode and binary output is encoded by Base64.
*
* @author JO Hyeong-ryeol
*/
public class StringEncrypter {
private Cipher rijndael;
private SecretKeySpec key;
private IvParameterSpec initalVector;
private static final String CIPHER_PROVIDER = "BC";

//Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

/**
* Creates a StringEncrypter instance.
*
* @param key A key string which is converted into UTF-8 and hashed by MD5.
* Null or an empty string is not allowed.
* @param initialVector An initial vector string which is converted into UTF-8
* and hashed by MD5. Null or an empty string is not allowed.
* @throws Exception
*/
public StringEncrypter(String key, String initialVector) throws Exception {
if (key == null || key.equals(""))
throw new Exception("The key can not be null or an empty string.");

if (initialVector == null || initialVector.equals(""))
throw new Exception("The initial vector can not be null or an empty string.");

if (Security.getProvider(CIPHER_PROVIDER) == null) {
Security.addProvider(new BouncyCastleProvider());
}

// Create a AES algorithm.
this.rijndael = Cipher.getInstance("AES/CBC/PKCS7Padding", CIPHER_PROVIDER );

// Initialize an encryption key and an initial vector.
this.key = new SecretKeySpec(key.getBytes("UTF8"), "AES");
this.initalVector = new IvParameterSpec(initialVector.getBytes("UTF8"));
/*MessageDigest md5 = MessageDigest.getInstance("MD5");
this.key = new SecretKeySpec(md5.digest(key.getBytes("UTF8")), "AES");
this.initalVector = new IvParameterSpec(md5.digest(initialVector.getBytes("UTF8")));*/
}

/**
* Encrypts a string.
*
* @param value A string to encrypt. It is converted into UTF-8 before being encrypted.
* Null is regarded as an empty string.
* @return An encrypted string.
* @throws Exception
*/
public String encrypt(String value) throws Exception {
if (value == null)
value = "";

// Initialize the cryptography algorithm.
this.rijndael.init(Cipher.ENCRYPT_MODE, this.key, this.initalVector);

// Get a UTF-8 byte array from a unicode string.
byte[] utf8Value = value.getBytes("UTF8");

// SHAUtil the UTF-8 byte array.
byte[] encryptedValue = this.rijndael.doFinal(utf8Value);

// Return a base64 encoded string of the encrypted byte array.
return Base64Encoder.encode(encryptedValue);
}

}
Loading

0 comments on commit 686be23

Please sign in to comment.