Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Commit

Permalink
fix major rsa public key encoding bug
Browse files Browse the repository at this point in the history
  • Loading branch information
bblfish committed Nov 17, 2015
1 parent 3ed67fa commit de8655e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
9 changes: 5 additions & 4 deletions app/rww/ldp/CertBinder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import org.w3.banana.{CertPrefix, RDF, RDFOps}
class CertBinder[Rdf <: RDF]()(implicit ops: RDFOps[Rdf], recordBinder: RecordBinder[Rdf]) {
import ops._
import recordBinder._
import utils.CryptoUtils.BigIntOps
val cert = CertPrefix[Rdf]

// implicit val rsaClassUri = classUrisFor[RSAPublicKey](cert.RSAPublicKey)
Expand All @@ -26,15 +27,15 @@ class CertBinder[Rdf <: RDF]()(implicit ops: RDFOps[Rdf], recordBinder: RecordBi

implicit val rsaPubKeybinder: PGBinder[Rdf, RSAPublicKey] =
pgb[RSAPublicKey](modulus, exponent)(
(m,e)=>factory.generatePublic(new RSAPublicKeySpec(new BigInteger(m),e)).asInstanceOf[RSAPublicKey],
key => Some((key.getModulus.toByteArray,key.getPublicExponent))
(m,e)=>factory.generatePublic(new RSAPublicKeySpec(new BigInteger(1,m),e)).asInstanceOf[RSAPublicKey],
key => Some((key.getModulus.toUnsignedByteArray(),key.getPublicExponent))
) // withClasses rsaClassUri

//todo: find better name for key
val rsaPubKeybinderWithHash: PGBinder[Rdf, RSAPublicKey] =
pgbWithId[RSAPublicKey](key=>URI("#key_"+Math.abs(key.getModulus.hashCode())))(modulus, exponent)(
(m,e)=>factory.generatePublic(new RSAPublicKeySpec(new BigInteger(m),e)).asInstanceOf[RSAPublicKey],
key => Some((key.getModulus.toByteArray,key.getPublicExponent))
(m,e)=>factory.generatePublic(new RSAPublicKeySpec(new BigInteger(1,m),e)).asInstanceOf[RSAPublicKey],
key => Some((key.getModulus.toUnsignedByteArray(),key.getPublicExponent))
) // withClasses rsaClassUri


Expand Down
26 changes: 26 additions & 0 deletions app/utils/CryptoUtils.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package utils

import java.math.BigInteger

/**
* Created by hjs on 17/11/2015.
*/
object CryptoUtils {

implicit class BigIntOps(big: BigInteger) {

/** the unsigned byte array for this bigInt */
def toUnsignedByteArray(): Array[Byte] = {
val bigBytes = big.toByteArray
if ((big.bitLength()%8) != 0) {
return bigBytes
} else {
val smallerBytes = new Array[Byte](big.bitLength()/8)
System.arraycopy(bigBytes,1,smallerBytes,0,smallerBytes.length)
smallerBytes
}
}
}


}

0 comments on commit de8655e

Please sign in to comment.