forked from apache/inlong
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[INLONG-10956][SDK] Transform SQL supports SHA encryption algorithm
- Loading branch information
ZKpLo
committed
Sep 3, 2024
1 parent
4de972e
commit 5e1469b
Showing
3 changed files
with
207 additions
and
0 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
...form-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/Sha2Function.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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. | ||
*/ | ||
|
||
package org.apache.inlong.sdk.transform.process.function; | ||
|
||
import org.apache.inlong.sdk.transform.decode.SourceData; | ||
import org.apache.inlong.sdk.transform.process.Context; | ||
import org.apache.inlong.sdk.transform.process.operator.OperatorTools; | ||
import org.apache.inlong.sdk.transform.process.parser.ValueParser; | ||
|
||
import net.sf.jsqlparser.expression.Expression; | ||
import net.sf.jsqlparser.expression.Function; | ||
import org.apache.commons.codec.digest.DigestUtils; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
|
||
import static org.apache.commons.codec.digest.MessageDigestAlgorithms.SHA_224; | ||
|
||
/** | ||
* Sha2Function | ||
* description: SHA2(str, hash_length): Calculates the SHA-2 family of hash functions (SHA-224, SHA-256, SHA-384, and SHA-512) | ||
* return NULL If either argument is NULL or the hash length(224 256 384 512) is not one of the permitted values | ||
* return a hash value containing the desired number of bits. | ||
*/ | ||
@TransformFunction(names = {"sha2"}) | ||
public class Sha2Function implements ValueParser { | ||
|
||
private final ValueParser msgParser; | ||
private final ValueParser lenParser; | ||
|
||
public Sha2Function(Function expr) { | ||
List<Expression> expressions = expr.getParameters().getExpressions(); | ||
msgParser = OperatorTools.buildParser(expressions.get(0)); | ||
lenParser = OperatorTools.buildParser(expressions.get(1)); | ||
} | ||
|
||
@Override | ||
public Object parse(SourceData sourceData, int rowIndex, Context context) { | ||
Object msgObj = msgParser.parse(sourceData, rowIndex, context); | ||
Object lenObj = lenParser.parse(sourceData, rowIndex, context); | ||
if (msgObj == null || lenObj == null) { | ||
return null; | ||
} | ||
String msg = msgObj.toString(); | ||
int len = Integer.parseInt(lenObj.toString()); | ||
switch (len) { | ||
case 0: | ||
case 256: | ||
return DigestUtils.sha256Hex(msg.getBytes(StandardCharsets.UTF_8)); | ||
case 224: | ||
return new DigestUtils(SHA_224).digestAsHex(msg.getBytes(StandardCharsets.UTF_8)); | ||
case 384: | ||
return DigestUtils.sha384Hex(msg.getBytes(StandardCharsets.UTF_8)); | ||
case 512: | ||
return DigestUtils.sha512Hex(msg.getBytes(StandardCharsets.UTF_8)); | ||
default: | ||
return null; | ||
} | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...sform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/ShaFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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. | ||
*/ | ||
|
||
package org.apache.inlong.sdk.transform.process.function; | ||
|
||
import org.apache.inlong.sdk.transform.decode.SourceData; | ||
import org.apache.inlong.sdk.transform.process.Context; | ||
import org.apache.inlong.sdk.transform.process.operator.OperatorTools; | ||
import org.apache.inlong.sdk.transform.process.parser.ValueParser; | ||
|
||
import net.sf.jsqlparser.expression.Function; | ||
import org.apache.commons.codec.digest.DigestUtils; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
|
||
/** | ||
* ShaFunction | ||
* description: sha(string): Compute the SHA-1 160 bit checksum of a string. | ||
* return NULL if the parameter is NULL | ||
* return a string of 40 hexadecimal digits. | ||
*/ | ||
@TransformFunction(names = {"sha"}) | ||
public class ShaFunction implements ValueParser { | ||
|
||
private final ValueParser msgParser; | ||
|
||
public ShaFunction(Function expr) { | ||
msgParser = OperatorTools.buildParser(expr.getParameters().getExpressions().get(0)); | ||
} | ||
|
||
@Override | ||
public Object parse(SourceData sourceData, int rowIndex, Context context) { | ||
Object msgObj = msgParser.parse(sourceData, rowIndex, context); | ||
if (msgObj == null) { | ||
return null; | ||
} | ||
String msg = msgObj.toString(); | ||
return DigestUtils.sha1Hex(msg.getBytes(StandardCharsets.UTF_8)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters