Skip to content

Commit

Permalink
WmSigner should use kwm byte array instead of kwm file path
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownNPC committed Aug 21, 2019
1 parent e3be458 commit a7d2e9c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 21 deletions.
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -45,10 +45,10 @@ sbt clean compile
```
#### Usage in java code
```
P24API p24Api = P24API.getInstance(1, "merchPass"));
QiwiAPI qiwiApi = QiwiAPI.getInstance("token");
P24API p24Api = P24API.getInstance(merchId, merchPass));
QiwiAPI qiwiApi = QiwiAPI.getInstance(qiwiApiToken);
// Requires KWM backup key(!)
WebMoneyAPI webMoneyApi = WebMoneyAPI.getInstance("wimd", "password", "/kwmPath/test.kwm");
WebMoneyAPI webMoneyApi = WebMoneyAPI.getInstance("wimd", "kwm_pass", kwmBytesArr);
```

#### API details
Expand Down
Expand Up @@ -84,14 +84,14 @@ class WebMoneyAPI(signer: WmSigner, wmid: String, httpClient: CloseableHttpClien

object WebMoneyAPI {

def apply(wmid: String, password: String, kwmPath: String,
def apply(wmid: String, password: String, kwmBytes: Array[Byte],
httpClient: CloseableHttpClient = InsecureHttpClient.getInstance()): WebMoneyAPI = {
val wmSigner = WmSigner(wmid, password, kwmPath)
val wmSigner = WmSigner(wmid, password, kwmBytes)

new WebMoneyAPI(wmSigner, wmid, httpClient)
}

def getInstance(wmid: String, password: String, kwmPath: String) =
apply(wmid, password, kwmPath)
def getInstance(wmid: String, password: String, kwmBytes: Array[Byte]) =
apply(wmid, password, kwmBytes)

}
Expand Up @@ -8,9 +8,7 @@ import javax.xml.bind.DatatypeConverter

import scala.util.{Random, Try}

private[wm] class WmSigner(wmid: String, pass: String, filePath: String, nextRandBytes: Array[Byte] => Unit = Random.nextBytes) {

private val kwmBytes: Array[Byte] = getFileBytes(filePath)
private[wm] class WmSigner(wmid: String, kwmPass: String, kwmBytes: Array[Byte], nextRandBytes: Array[Byte] => Unit = Random.nextBytes) {

private val reservedBytes: Array[Byte] = kwmBytes.slice(0, 2)
private val crc: Array[Byte] = kwmBytes.slice(4, 20)
Expand Down Expand Up @@ -82,16 +80,9 @@ private[wm] class WmSigner(wmid: String, pass: String, filePath: String, nextRan
(0 until length).toList.foldLeft(Array[Byte]()) { case (array, index) => array :+ target(length - 1 - index) }
}

private def getFileBytes(resource: String): Array[Byte] = {

import java.nio.file.{Files, Paths}

Files.readAllBytes(Paths.get(getClass.getResource(resource).toURI))
}

// Weird staff is living here.
private def getSecureBuffer(buffer: Array[Byte]): Array[Byte] = {
val md4hashVal: Array[Byte] = Utils.md4Hash((wmid + pass).getBytes)
val md4hashVal: Array[Byte] = Utils.md4Hash((wmid + kwmPass).getBytes)
val result = Array[Byte]().patch(0, buffer, 6)
var x = 0
for (i <- 6 until buffer.length) yield {
Expand All @@ -118,6 +109,7 @@ private[wm] class WmSigner(wmid: String, pass: String, filePath: String, nextRan

object WmSigner {

def apply(wmid: String, pass: String, filePath: String): WmSigner = new WmSigner(wmid, pass, filePath)
def apply(wmid: String, kwmPass: String, kwmBytes: Array[Byte]): WmSigner =
new WmSigner(wmid, kwmPass, kwmBytes)

}
Expand Up @@ -11,12 +11,17 @@ class WmSignerTest extends FunSpec with Matchers {
private val testSignature = "642c2f71aafe930bcd238d925833414ccba29f2d408f3b77f1d7100111269865a100c6550258420734e96b4c11153ed597af9a28a066ffece8b50b0c5ffa15fe068f"

it("should sign string correctly") {
val signerWithoutRandom = new WmSigner(wmid, keyPassword, keyFilePath, nextRandBytes = _ => ())
val signerWithoutRandom = new WmSigner(wmid, keyPassword, getFileBytes(keyFilePath), nextRandBytes = _ => ())
signerWithoutRandom.sign(testString) shouldBe Right(testSignature)

val signerWithRandom = new WmSigner(wmid, keyPassword, keyFilePath)
val signerWithRandom = new WmSigner(wmid, keyPassword, getFileBytes(keyFilePath))
val signatureWithRand = signerWithRandom.sign(testString)
signatureWithRand.right.get should have length 132
}

def getFileBytes(resource: String): Array[Byte] = {
import java.nio.file.{Files, Paths}
Files.readAllBytes(Paths.get(getClass.getResource(resource).toURI))
}

}

0 comments on commit a7d2e9c

Please sign in to comment.