Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to set MD5 and SHA1 encryption for image recognition using Vuforia rest Api ?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 463
    Comment on it

    In our previous tutorial we learn how to configure vuforia for Cloud recognition, in this tutorial we will learn about how to set encryption technique while hitting rest apis of Vuforia and in our next tutorial we will share the basic http request.

    Step 1. First of all we will send http request in this method to get its type, Vuforia apis uses post method of http with multipart so we have to check that in request and set respective content type like multipart/form-data.

    public String tmsSignature(HttpUriRequest request, String secretKey) {
          String method = request.getMethod();
          String contentType = "";
          String hexDigest = "d41d8cd98f00b204e9800998ecf8427e"; // Hex digest of an empty string
    
          if (method.equalsIgnoreCase("GET") || method.equalsIgnoreCase("DELETE")) {
             // Do nothing because the strings are already set correctly
          } else if (method.equalsIgnoreCase("POST") || method.equalsIgnoreCase("PUT")) {
             contentType = "multipart/form-data";
             // If this is a POST or PUT the request should have a request body
             hexDigest = contentMD5((HttpEntityEnclosingRequestBase) request);
          } else {
             System.out.println("ERROR: Invalid content type passed to Sig Builder");
          }
    
          // Date in the header and date used to calculate the hash must be the same
          String dateValue = request.getFirstHeader("Date").getValue();
          Log.e("Date: ", dateValue);
          String requestPath = request.getURI().getPath();
          String toDigest = new String(method + "\n" + hexDigest + "\n" + contentType + "\n" + dateValue + "\n" + requestPath);
          String shaHashed = "";
          try {
             shaHashed = calculateRFC2104HMAC(secretKey, toDigest);
    
             Log.e("Signature", toDigest);
             Log.e("hash Signature", shaHashed);
    
          } catch (SignatureException e) {
             e.printStackTrace();
          }
          return shaHashed;
       }
    

    Step 2. Now we will encrypt http request using md5 technique like this :

    private String contentMD5(HttpEntityEnclosingRequestBase httpMethod) {
       ByteArrayOutputStream requestOutputStream = new ByteArrayOutputStream();
       try {
          httpMethod.getEntity().writeTo(requestOutputStream);
       } catch (IOException e) {
          System.out.println("ERROR: IOException caught when writing Content MD5 hash");
          e.printStackTrace();
       }
    
       return DigestUtils.md5Hex(requestOutputStream.toByteArray()).toLowerCase();
    }

    but for this you have to add some jar files to perform Digest Utils functions like md5Hex i.e commons-codec-1.3.jar

    Step 3. Now calculate calculateRFC2104HMAC for secret key and the whole digest string :

    public static String calculateRFC2104HMAC(String key, String data) throws SignatureException {
       String result = "";
       try {
          // get an hmac_sha1 key from the raw key bytes
          SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), "HmacSHA1");
    
          // get an hmac_sha1 Mac instance and initialize with the signing key
          Mac mac = Mac.getInstance("HmacSHA1");
          mac.init(signingKey);
    
          // compute the hmac on input data bytes
          byte[] rawHmac = mac.doFinal(data.getBytes());
    
          // base64-encode the hmac
          result = new String(Base64.encodeBase64(rawHmac, false));
    
       } catch (Exception e) {
          e.printStackTrace();
       }
       return result;
    }

    With the help of these signature you can create same header mentioned in vuforia api here :

    https://library.vuforia.com/articles/Solution/How-To-Perform-an-Image-Recognition-Query

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: