class SecurityIdentifier {

  public static byte[] toBytes(final String sid)
  {
    // CheckStyle:MagicNumber OFF

    // format of SID: S-R-X-Y1-Y2...-Yn
    // S: static 'S', indicating string
    // R: revision
    // X: authority
    // Yn: sub-authority

    final StringTokenizer st = new StringTokenizer(sid, "-");
    // first token is the 'S'
    st.nextToken();

    // second token is the revision
    final int revision = Integer.valueOf(st.nextToken());
    // third token is the authority
    final long authority = Long.valueOf(st.nextToken());
    // remaining token are the sub authorities
    final List<String> subAuthorities = new ArrayList<>();
    while (st.hasMoreTokens()) {
      subAuthorities.add(st.nextToken());
    }

    // revision is 1 byte
    // sub-authorities count is 1 byte
    // authority is 6 bytes
    // 4 bytes for each sub-authority
    final int size = 8 + (4 * subAuthorities.size());
    final ByteBuffer sidBuffer = ByteBuffer.allocate(size);
    sidBuffer.put((byte) (revision & 0xFF));
    sidBuffer.put((byte) (subAuthorities.size() & 0xFF));
    sidBuffer.limit(8);
    putLong(sidBuffer, authority, true);
    for (String subAuthority : subAuthorities) {
      sidBuffer.limit(sidBuffer.position() + 4);
      putLong(sidBuffer, Long.valueOf(subAuthority), false);
    }

    return sidBuffer.array();
    // CheckStyle:MagicNumber ON
  }

  private  SecurityIdentifier();

}