Skip to content

Commit

Permalink
feat: create MessageDigest when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelAndalon committed Jan 3, 2024
1 parent 4ca522a commit a8778a1
Showing 1 changed file with 6 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
/*
* Copyright 2022 Expedia, Inc
*
* 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
*
* https://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 com.expediagroup.graphql.apq.extensions

import com.expediagroup.graphql.apq.provider.AutomaticPersistedQueriesExtension
Expand All @@ -23,7 +7,6 @@ import java.nio.charset.StandardCharsets
import java.security.MessageDigest

internal const val APQ_EXTENSION_KEY: String = "persistedQuery"
internal val MESSAGE_DIGEST: MessageDigest = MessageDigest.getInstance("SHA-256")

@Suppress("UNCHECKED_CAST")
fun ExecutionInput.getAutomaticPersistedQueriesExtension(): AutomaticPersistedQueriesExtension? =
Expand All @@ -34,13 +17,13 @@ fun ExecutionInput.getAutomaticPersistedQueriesExtension(): AutomaticPersistedQu
null
}

fun ExecutionInput.getQueryId(): String =
String.format(
fun ExecutionInput.getQueryId(): String {
val messageDigest = MessageDigest.getInstance("SHA-256")
return String.format(
"%064x",
BigInteger(1, MESSAGE_DIGEST.digest(this.query.toByteArray(StandardCharsets.UTF_8)))
).also {
MESSAGE_DIGEST.reset()
}
BigInteger(1, messageDigest.digest(this.query.toByteArray(StandardCharsets.UTF_8)))
)
}

fun ExecutionInput.isAutomaticPersistedQueriesExtensionInvalid(
extension: AutomaticPersistedQueriesExtension
Expand Down

0 comments on commit a8778a1

Please sign in to comment.