@Override
 public V replace(@NotNull K key, @NotNull V value) {
   long h = hasher.hash(key);
   int segment = hasher.getSegment();
   segments[segment].put(h, key, value, true, false);
   return null;
 }
 @Override
 public V remove(Object key) {
   long h = hasher.hash(key);
   int segment = hasher.getSegment();
   segments[segment].remove(h, (K) key);
   return null;
 }
 @Override
 public V put(K key, V value) {
   long h = hasher.hash(key);
   int segment = hasher.getSegment();
   segments[segment].put(h, key, value, true, true);
   return null;
 }
Ejemplo n.º 4
0
 /**
  * Add new node to NodeList(treemap)
  *
  * @param address String ipaddress
  * @throws Exception Exception if allready in list
  */
 public void addNode(InetAddress address, String Naam) {
   Hasher myHasher = new Hasher();
   int nodeID = myHasher.Hash(Naam);
   if (!NodeList.containsKey(nodeID) && !NodeList.containsValue(address)) {
     NodeList.put(nodeID, address);
   }
 }
Ejemplo n.º 5
0
 HashCode makeHash(Hasher[] hashers) {
   byte[] bytes = new byte[this.bits / 8];
   int i = 0;
   for (Hasher hasher : hashers) {
     HashCode newHash = hasher.hash();
     i += newHash.writeBytesTo(bytes, i, newHash.bits() / 8);
   }
   return HashCode.fromBytesNoCopy(bytes);
 }
Ejemplo n.º 6
0
  public Hasher forType(final PasswordType passwordType) {
    notNull(passwordType, "passwordType may not be null");

    for (Hasher hasher : hashers) {
      if (passwordType == hasher.getType()) {
        return hasher;
      }
    }

    throw new IllegalArgumentException("No hasher available for type " + passwordType);
  }
Ejemplo n.º 7
0
 /**
  * Find the IP address of the node that has the specified file
  *
  * @param FileName name of the file that has to be found
  * @return null if no nodes in the system, else return IP address of the node.
  */
 public InetAddress getLocation(String FileName) {
   if (NodeList.isEmpty()) {
     return null;
   }
   Hasher myHasher = new Hasher();
   int fileID = myHasher.Hash(FileName);
   Integer NodeID = NodeList.ceilingKey(fileID);
   if (NodeID == null) {
     NodeID = NodeList.firstKey();
   }
   return NodeList.get(NodeID);
 }
  public void testHashTwice() {
    Hasher sha1 = Hashing.sha1().newHasher();

    assertEquals(
        "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12",
        sha1.putString("The quick brown fox jumps over the lazy dog", Charsets.UTF_8)
            .hash()
            .toString());
    try {
      sha1.hash();
      fail();
    } catch (IllegalStateException expected) {
    }
  }
 @Override
 public int hashCode() {
   HashFunction hf = Hashing.goodFastHash(32);
   Hasher h = hf.newHasher();
   h.putInt(slots.size());
   for (int i = 0; i < slots.size(); i++) {
     h.putInt(slots.get(i).size());
     for (int j = 0; j < slots.size(); j++) {
       h.putBytes(slots.get(i).get(j).getLowerRange());
       h.putBytes(slots.get(i).get(j).getUpperRange());
     }
   }
   return h.hash().asInt();
 }
 @Override
 public boolean replace(@NotNull K key, @NotNull V oldValue, @NotNull V newValue) {
   long h = hasher.hash(key);
   int segment = hasher.getSegment();
   Segment<K, V> segment2 = segments[segment];
   //noinspection SynchronizationOnLocalVariableOrMethodParameter
   synchronized (segment2) {
     V value2 = get(key);
     if (value2 != null && oldValue.equals(value2)) {
       segment2.put(h, key, newValue, true, true);
       return true;
     }
     return false;
   }
 }
 @Override
 public boolean remove(@NotNull Object key, Object value) {
   long h = hasher.hash(key);
   int segment = hasher.getSegment();
   Segment<K, V> segment2 = segments[segment];
   //noinspection SynchronizationOnLocalVariableOrMethodParameter
   synchronized (segment2) {
     V value2 = get(key);
     if (value2 != null && value.equals(value2)) {
       segment2.remove(h, (K) key);
       return true;
     }
     return false;
   }
 }
 public Entry<K, V> getNextEntry(K prevKey) {
   try {
     int pos;
     if (prevKey == null) {
       pos = smallMap.firstPos();
     } else {
       long hash = hasher.hash(prevKey);
       pos = smallMap.nextDifferentHashNonEmptyPosition(hash);
     }
     while (true) {
       if (pos < 0) {
         return null;
       } else {
         bytes.storePositionAndSize(store, pos * smallEntrySize, smallEntrySize);
         K key = getKey();
         if (prevKey == null || !equals(key, prevKey)) {
           if (bytesMarshallable) {
             V value = (V) NativeBytes.UNSAFE.allocateInstance(vClass);
             ((BytesMarshallable) value).readMarshallable(bytes);
             return new SimpleEntry<K, V>(key, value);
           } else {
             V value = (V) bytes.readObject();
             return new SimpleEntry<K, V>(key, value);
           }
         }
       }
       pos = smallMap.nextPos();
     }
   } catch (InstantiationException e) {
     throw new AssertionError(e);
   }
 }
Ejemplo n.º 13
0
 @Override
 public boolean remove(Object o) {
   final int hash = hasher.getHashCode(o);
   if (map.containsKey(hash)) {
     super.remove(map.get(hash));
     map.remove(hash);
     return true;
   } else {
     return false;
   }
 }
Ejemplo n.º 14
0
 @Override
 public boolean add(T o) {
   final int hash = hasher.getHashCode(o);
   if (!map.containsKey(hash)) {
     map.put(hash, o);
     super.add(o);
     return true;
   } else {
     return false;
   }
 }
  /** @see niagara.query_engine.PhysicalOperator#processTuple(Tuple, int) */
  protected void processTuple(Tuple tuple, int streamId)
      throws ShutdownException, InterruptedException {
    String hash = hasher.hashKey(tuple);

    // ignore null attributes...
    if (hash == null) return;

    // Have we seen this group before?
    Tuple representativeTuple = (Tuple) hash2tuple.get(hash);
    if (representativeTuple == null) {
      // Output tuples contain the groupby attributes plus
      // two more fields: previous and current group info
      representativeTuple = new Tuple(true, groupAttrs.length + 2);
      // Copy the groupby attributes
      for (int i = 0; i < groupAttrs.length; i++) {
        representativeTuple.appendAttribute(tuple.getAttribute(groupAttrs[i]));
      }
      // Initialize the group hashtable entries
      hash2partial.put(hash, emptyGroupValue());
      hash2final.put(hash, emptyGroupValue());
      hash2tuple.put(hash, representativeTuple);
    }

    Tuple newTuple = new Tuple(tuple.isPartial(), groupAttrs.length + 2);
    newTuple.appendTuple(representativeTuple);

    Node oldResult = null;
    Node newResult;
    // partial tuples only affect partial results
    Object oldGroupInfo = hash2partial.get(hash);
    if (logicalGroupOperator.outputOldValue()) oldResult = constructOutput(oldGroupInfo);
    Object newGroupInfo = processTuple(tuple, oldGroupInfo);
    newResult = constructOutput(newGroupInfo);
    hash2partial.put(hash, newGroupInfo);
    // final tuples affect both final and partial results
    if (!tuple.isPartial()) {
      // final tuples affect both final and partial results
      oldGroupInfo = hash2final.get(hash);
      if (logicalGroupOperator.outputOldValue()) oldResult = constructOutput(oldGroupInfo);
      newGroupInfo = processTuple(tuple, hash2final.get(hash));
      newResult = constructOutput(newGroupInfo);
      hash2final.put(hash, newGroupInfo);
    }
    if (newGroupInfo == oldGroupInfo) {
      // There was no change in the group,
      // do not output a tuple
      return;
    }
    if (logicalGroupOperator.outputOldValue()) newTuple.appendAttribute(oldResult);
    newTuple.appendAttribute(newResult);
    putTuple(newTuple, 0);
  }
Ejemplo n.º 16
0
 public FileCollectionSnapshot snapshot(FileCollection sourceFiles) {
   Map<String, FileSnapshot> snapshots = new HashMap<String, FileSnapshot>();
   for (File file : sourceFiles) {
     if (file.isFile()) {
       snapshots.put(file.getAbsolutePath(), new FileHashSnapshot(hasher.hash(file)));
     } else if (file.isDirectory()) {
       snapshots.put(file.getAbsolutePath(), new DirSnapshot());
     } else {
       snapshots.put(file.getAbsolutePath(), new MissingFileSnapshot());
     }
   }
   return new FileCollectionSnapshotImpl(snapshots);
 }
  /**
   * This function initializes the data structures for an operator. This over-rides the
   * corresponding function in the base class.
   *
   * @return True if the operator is to continue and false otherwise
   */
  protected void opInitialize() {
    // Get the grouping attributes
    skolem grouping = logicalGroupOperator.getSkolemAttributes();

    Vector groupVars = grouping.getVarList();
    hasher = new Hasher(groupVars);

    groupAttrs = new int[groupVars.size()];
    for (int i = 0; i < groupAttrs.length; i++) {
      groupAttrs[i] = inputTupleSchemas[0].getPosition(((Attribute) groupVars.get(i)).getName());
    }

    hasher.resolveVariables(inputTupleSchemas[0]);

    // Initialize the hash tables
    hash2final = new HashMap();
    hash2partial = new HashMap();
    hash2tuple = new HashMap();
  }
 public K getNextKey(K prevKey) {
   int pos;
   if (prevKey == null) {
     pos = smallMap.firstPos();
   } else {
     long hash = hasher.hash(prevKey);
     pos = smallMap.nextDifferentHashNonEmptyPosition(hash);
   }
   while (true) {
     if (pos < 0) {
       return null;
     } else {
       bytes.storePositionAndSize(store, pos * smallEntrySize, smallEntrySize);
       K key = getKey();
       if (prevKey == null || !equals(key, prevKey)) {
         return key;
       }
     }
     pos = smallMap.nextPos();
   }
 }
Ejemplo n.º 19
0
 public HashCode hash() {
   return hasher.hash();
 }
Ejemplo n.º 20
0
 public void testGetHash() throws Exception {
   String passhash = "AVM1KtaDI8oCfzIHasfN";
   String compare = Hasher.getHash("password");
   assertEquals("Password hashes were not equal", passhash, compare);
 }
Ejemplo n.º 21
0
 @Test
 public void testHash() {
   byte[] hash = Hasher.generateHash("foobar".getBytes());
   assertEquals("8843d7f92416211de9ebb963ff4ce28125932878", new NodeKey(hash).toString());
 }
  // Do an update from the given repo. All applications found, and their
  // APKs, are added to 'apps'. (If 'apps' already contains an app, its
  // APKs are merged into the existing one).
  // Returns null if successful, otherwise an error message to be displayed
  // to the user (if there is an interactive user!)
  // 'newetag' should be passed empty. On success, it may contain an etag
  // value for the index that was successfully processed, or it may contain
  // null if none was available.
  public static String doUpdate(
      Context ctx,
      DB.Repo repo,
      List<DB.App> apps,
      StringBuilder newetag,
      List<Integer> keeprepos,
      ProgressListener progressListener) {
    try {

      int code = 0;
      if (repo.pubkey != null) {

        // This is a signed repo - we download the jar file,
        // check the signature, and extract the index...
        Log.d(
            "FDroid",
            "Getting signed index from "
                + repo.address
                + " at "
                + logDateFormat.format(new Date(System.currentTimeMillis())));
        String address = repo.address + "/index.jar";
        PackageManager pm = ctx.getPackageManager();
        try {
          PackageInfo pi = pm.getPackageInfo(ctx.getPackageName(), 0);
          address += "?" + pi.versionName;
        } catch (Exception e) {
        }
        Bundle progressData = createProgressData(repo.address);
        ProgressListener.Event event =
            new ProgressListener.Event(RepoXMLHandler.PROGRESS_TYPE_DOWNLOAD, progressData);
        code =
            getRemoteFile(
                ctx, address, "tempindex.jar", repo.lastetag, newetag, progressListener, event);
        if (code == 200) {
          String jarpath = ctx.getFilesDir() + "/tempindex.jar";
          JarFile jar = null;
          JarEntry je;
          Certificate[] certs;
          try {
            jar = new JarFile(jarpath, true);
            je = (JarEntry) jar.getEntry("index.xml");
            File efile = new File(ctx.getFilesDir(), "/tempindex.xml");
            InputStream input = null;
            OutputStream output = null;
            try {
              input = jar.getInputStream(je);
              output = new FileOutputStream(efile);
              Utils.copy(input, output);
            } finally {
              Utils.closeQuietly(output);
              Utils.closeQuietly(input);
            }
            certs = je.getCertificates();
          } catch (SecurityException e) {
            Log.e("FDroid", "Invalid hash for index file");
            return "Invalid hash for index file";
          } finally {
            if (jar != null) {
              jar.close();
            }
          }
          if (certs == null) {
            Log.d("FDroid", "No signature found in index");
            return "No signature found in index";
          }
          Log.d(
              "FDroid",
              "Index has " + certs.length + " signature" + (certs.length > 1 ? "s." : "."));

          boolean match = false;
          for (Certificate cert : certs) {
            String certdata = Hasher.hex(cert.getEncoded());
            if (repo.pubkey.equals(certdata)) {
              match = true;
              break;
            }
          }
          if (!match) {
            Log.d("FDroid", "Index signature mismatch");
            return "Index signature mismatch";
          }
        }

      } else {

        // It's an old-fashioned unsigned repo...
        Log.d("FDroid", "Getting unsigned index from " + repo.address);
        Bundle eventData = createProgressData(repo.address);
        ProgressListener.Event event =
            new ProgressListener.Event(RepoXMLHandler.PROGRESS_TYPE_DOWNLOAD, eventData);
        code =
            getRemoteFile(
                ctx,
                repo.address + "/index.xml",
                "tempindex.xml",
                repo.lastetag,
                newetag,
                progressListener,
                event);
      }

      if (code == 200) {
        // Process the index...
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();
        RepoXMLHandler handler = new RepoXMLHandler(repo, apps, progressListener);
        xr.setContentHandler(handler);

        File tempIndex = new File(ctx.getFilesDir() + "/tempindex.xml");
        BufferedReader r = new BufferedReader(new FileReader(tempIndex));

        // A bit of a hack, this might return false positives if an apps description
        // or some other part of the XML file contains this, but it is a pretty good
        // estimate and makes the progress counter more informative.
        // As with asking the server about the size of the index before downloading,
        // this also has a time tradeoff. It takes about three seconds to iterate
        // through the file and count 600 apps on a slow emulator (v17), but if it is
        // taking two minutes to update, the three second wait may be worth it.
        final String APPLICATION = "<application";
        handler.setTotalAppCount(Utils.countSubstringOccurrence(tempIndex, APPLICATION));

        InputSource is = new InputSource(r);
        xr.parse(is);

        if (handler.pubkey != null && repo.pubkey == null) {
          // We read an unsigned index, but that indicates that
          // a signed version is now available...
          Log.d("FDroid", "Public key found - switching to signed repo for future updates");
          repo.pubkey = handler.pubkey;
          try {
            DB db = DB.getDB();
            db.updateRepoByAddress(repo);
          } finally {
            DB.releaseDB();
          }
        }

      } else if (code == 304) {
        // The index is unchanged since we last read it. We just mark
        // everything that came from this repo as being updated.
        Log.d("FDroid", "Repo index for " + repo.address + " is up to date (by etag)");
        keeprepos.add(repo.id);
        // Make sure we give back the same etag. (The 200 route will
        // have supplied a new one.
        newetag.append(repo.lastetag);

      } else {
        return "Failed to read index - HTTP response " + Integer.toString(code);
      }

    } catch (SSLHandshakeException sslex) {
      Log.e(
          "FDroid",
          "SSLHandShakeException updating from "
              + repo.address
              + ":\n"
              + Log.getStackTraceString(sslex));
      return "A problem occurred while establishing an SSL connection. If this problem persists, AND you have a very old device, you could try using http instead of https for the repo URL.";
    } catch (Exception e) {
      Log.e(
          "FDroid", "Exception updating from " + repo.address + ":\n" + Log.getStackTraceString(e));
      return "Failed to update - " + e.getMessage();
    } finally {
      ctx.deleteFile("tempindex.xml");
      ctx.deleteFile("tempindex.jar");
    }

    return null;
  }
 @Override
 public V get(K key, V value) {
   long h = hasher.hash(key);
   int segment = hasher.getSegment();
   return segments[segment].get(h, key, value);
 }
Ejemplo n.º 24
0
 @Override
 public boolean contains(Object o) {
   return map.containsKey(hasher.getHashCode(o));
 }
 @Override
 public boolean containsKey(Object key) {
   long h = hasher.hash(key);
   int segment = hasher.getSegment();
   return segments[segment].containsKey(h, (K) key);
 }
  /**
   * saves the edited user to elastic search.
   *
   * @param view the view that is going to be used to change the user.
   */
  @SuppressWarnings({"unused", "UnusedParameters"})
  public void editUser(View view) {
    // implements US 03.02.01
    EditText editUserName = (EditText) findViewById(R.id.editUserName);
    EditText editUserEmail = (EditText) findViewById(R.id.editUserEmail);
    EditText editUserAddress1 = (EditText) findViewById(R.id.editUserAddress1);
    EditText editUserAddress2 = (EditText) findViewById(R.id.editUserAddress2);
    EditText editUserCity = (EditText) findViewById(R.id.editUserCity);
    EditText editUserPhone = (EditText) findViewById(R.id.editUserPhone);
    EditText editUserPostalCode = (EditText) findViewById(R.id.editUserPostalCode);
    EditText editUserPassword1 = (EditText) findViewById(R.id.editUserPassword1);
    EditText editUserPassword2 = (EditText) findViewById(R.id.editUserPassword2);
    EditText editUserPassword3 = (EditText) findViewById(R.id.editUserPassword3);

    user.setName(editUserName.getText().toString());
    user.setEmail(editUserEmail.getText().toString());
    user.setAddress1(editUserAddress1.getText().toString());
    user.setAddress2(editUserAddress2.getText().toString());
    user.setCity(editUserCity.getText().toString());
    user.setPhone(editUserPhone.getText().toString());
    user.setPostal(editUserPostalCode.getText().toString());

    String pass1 = editUserPassword1.getText().toString();
    String pass2 = editUserPassword2.getText().toString();
    String pass3 = editUserPassword3.getText().toString();

    if (action != null && action.equals("NEW")) {
      user.setPasshash(Hasher.getHash(pass1));
      ElasticSearcher.sendUser(user);
      CharSequence text = "User account created!";
      int duration = Toast.LENGTH_SHORT;
      Toast toast = Toast.makeText(this, text, duration);
      toast.show();
      finish();
    } else {
      if (pass1.equals("") && pass2.equals("") && pass3.equals("")) {
        ElasticSearcher.sendUser(user);
        CharSequence text = "Saved!";
        int duration = Toast.LENGTH_SHORT;
        Toast toast = Toast.makeText(this, text, duration);
        toast.show();
        return;
      }

      if (!Hasher.getHash(pass1).equals(user.getPasshash())) {
        editUserPassword1.setError("The entered password is incorrect.");
        return;
      }

      if (!Constants.isPasswordValid(pass2)) {
        editUserPassword2.setError("The chosen password is invalid.");
        return;
      }

      if (!pass2.equals(pass3)) {
        editUserPassword3.setError("The new passwords do not match.");
        return;
      }

      user.setPasshash(Hasher.getHash(pass2));
      ElasticSearcher.sendUser(user);
      CharSequence text = "Saved!";
      int duration = Toast.LENGTH_SHORT;
      Toast toast = Toast.makeText(this, text, duration);
      toast.show();
      editUserPassword1.setText("");
      editUserPassword2.setText("");
      editUserPassword3.setText("");
    }
  }
Ejemplo n.º 27
0
 public void write(int i) throws IOException {
   hasher.putByte((byte) i);
   out.write(i);
 }
Ejemplo n.º 28
0
 public void write(byte abyte0[], int i, int j) throws IOException {
   hasher.putBytes(abyte0, i, j);
   out.write(abyte0, i, j);
 }