haridsv commented on code in PR #2379: URL: https://github.com/apache/phoenix/pull/2379#discussion_r3013746484
########## phoenix-core-client/src/main/java/org/apache/phoenix/util/SHA256DigestUtil.java: ########## @@ -0,0 +1,100 @@ +/* + * 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.phoenix.util; + +import java.io.ByteArrayInputStream; +import java.io.DataInputStream; +import java.io.IOException; +import java.nio.ByteBuffer; +import org.apache.hadoop.hbase.util.Bytes; +import org.bouncycastle.crypto.digests.SHA256Digest; + +/** + * Utility class for SHA-256 digest state serialization and deserialization. We are not using jdk + * bundled SHA, since their digest can't be serialized/deserialized which is needed for + * PhoenixSyncTableTool for cross-region hash continuation. + */ +public class SHA256DigestUtil { + + /** + * Maximum allowed size for encoded SHA-256 digest state. SHA-256 state is ~96 bytes, we allow up + * to 128 bytes as buffer. + */ + public static final int MAX_SHA256_DIGEST_STATE_SIZE = 128; + + /** + * Encodes a SHA256Digest state to a byte array with length prefix for validation. Format: [4-byte + * integer length][encoded digest state bytes] + * @param digest The digest whose state should be encoded + * @return Byte array containing integer length prefix + encoded state + */ + public static byte[] encodeDigestState(SHA256Digest digest) { + byte[] encoded = digest.getEncodedState(); + ByteBuffer buffer = ByteBuffer.allocate(Bytes.SIZEOF_INT + encoded.length); + buffer.putInt(encoded.length); + buffer.put(encoded); + return buffer.array(); + } Review Comment: I am not sure what you mean, where else is this util useful and why is encoding the length important in those cases? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
